Not able to access correct gameobject to modify variables.

I know what is happening, probably even why, but i’m unsure how to solve it.

I have an object spawner, an object and a player. On my player, there is a script which controls my ability to move which includes a “can move” bool.

Now when my spawned object collides with my player my played is supposed to be stunned by deactivating the “can move” property in the script of my player.

This is the code,

public class FallingObjectControl : MonoBehaviour {
    //Get the Character
    public GameObject playerObject;
    public bool Stunned;
    public float StunnedFor = 5.0f;
    // Use this for initialization
    void Start () {
    }
    void FixedUpdate()
    {
        if (Stunned)
        {
            Debug.Log("Stunned");
            //  HandleStun();
            StartCoroutine(HandleStun());
        }
    }
    void Update () {
      
    }
    private IEnumerator HandleStun()
    {
        //Stunned, De activate movement
        playerObject.GetComponent<CharacterHorizontalMovement>().AbilityPermitted = false;
        yield return new WaitForSeconds(StunnedFor);
        playerObject.GetComponent<CharacterHorizontalMovement>().AbilityPermitted = true;
        Stunned = false;
      
    }

Problem
My problem is not the inability to access variables from another script, I think it’s with assigning the correct game object.

The problem I am having is that, if I select the FallingObject prefab, I cannot drag my player in my hierarchy view to the public game object. I believe I read that was because you can have different game objects in different levels.

If I drag the prefab from my player character into the public field for the game object, it accepts it, the code is run and “Stunned” shows up in my debug log, but the bool is NOT set to false making me think that the game object that I dragged in is not the same game object as my player.

HOWEVER, if i drag out the falling object to my hierarchy and drag the game object from my hierarchy into public field, it works perfectly when it collides with that single object.

I would like some help in resolving this, from my testing it seems to be some issue between accessing the right game object, but im not sure what game object it is expecting to work.

I have a bigger question: What is FallingObjectControl attached to?

If it’s attached to the player, you’ve already got the player gameobject. No need to try and store it in a separate gameobject member.

If it’s attached to objects that collide with the player, why is it handling the player’s stun state? The player should handle its own stun state. Imagine if 5 different objects hit the player at once. Suddenly you have 5 coroutines trying to manipulate the player’s stun state.

Finally, what is setting the Stunned flag? Is it from some collision code not included in what you posted here? Typically you can get a reference to the collider gameobject from the collision callback.

FallingObjectControl is attached to the items that spawn and fall.
I will take your advice and move it to the player, however it is an “enemy” . I was thinking, if I have 5 different enemies, Should the player know of all 5 of them? or allow them to access the player’s attributes?

Added the stun flag

    void OnTriggerEnter2D(Collider2D col)
    {

        if (col.gameObject.name == "spine-space-chicken")
        {
            Debug.Log("Collided with Character FallingObjectcontrol");
            Stunned = true;

        }
    
    }

Players, enemies, and objects should only know about themselves – how much health they have, what animation (if any) is currently playing, current state, etc. There’s no need for an object to know whether the player was sleeping, sitting, walking, etc. All it needs to know is that it hit the player.

If the player has an OnTriggerEnter2D function, you can check if it collided with a falling object using this code:

var fallingObject = col.GetComponent<FallingObjectControl>();
if (fallingObject != null)
{
    Debug.Log("Collided with a FallingObjectControl"); 
    Stunned = true;
}

You can do the same with some enemy script, etc. If GetComponent() returns null, then that object is not a whatevertype type.

I’d say reading attributes is fine if you need to know more info such as was it a fire damage type, electric, etc. You just don’t try to control/manipulate other objects – that can get messy fast.