I can't change bool variable in other script. C#

Hi, I’m learning coding in C# in Unity. I’ve got a Will Goldstone Book about Unity. But I have a little problem. I created ‘Create Empty’ Game Object as a Spawn Point for my First Aid Capsule. In this Empty game object ( firstManager ) I attached that script: //firstManager have tag: firstAidC

using UnityEngine;

public class firstManager : MonoBehaviour
{
    public PlayerHealth playerHealth;
    public GameObject firstAid;
    public float spawnTime = 1f;
    public Transform[] spawnPoints;
    public bool exist = false;

    void Start ()
    {
            InvokeRepeating ("Spawn", spawnTime, spawnTime);
    }
    void Spawn ()
    {
                if (playerHealth.currentHealth <= 0f)
                {
                        return;
                }
        if (exist != true)
        {
            int spawnPointIndex = Random.Range (0, spawnPoints.Length);
        
            Instantiate (firstAid, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
        }
        exist = true;   
    }
}

And at ‘FirstAidCapsuleRotator’ ( this is a prefab ) i’ve:

using UnityEngine;
using System.Collections;

public class FirstAidCapsuleRotator : MonoBehaviour {

    GameObject player;
    PlayerHealth playerHealth;
    bool playerInRange;
    public int howMuchHeal=20;



    void Awake ()
    {

        player = GameObject.FindGameObjectWithTag ("Player");
        playerHealth = player.GetComponent <PlayerHealth> ();
    }



    void OnTriggerEnter (Collider other)
    {
        if(other.gameObject == player)
        {
            playerInRange = true;
        }
    }

    void OnTriggerExit (Collider other)
    {
        if(other.gameObject == player)
        {
            playerInRange = false;
        }
    }

    void Update ()
    {
        transform.Rotate (new Vector3 (20, 10, 0) * Time.deltaTime * 10);

        if(playerHealth.currentHealth < 100 && playerInRange)
        {

            Heal ();

        }

    }

    void Heal ()
    {
        if(playerHealth.currentHealth < 100)
        {
            playerHealth.Healed(howMuchHeal);
            Destroy (gameObject);
            //GameObject.FindWithTag("firstAidC").GetComponent<firstManager>().exist = false;
        }

    }

}

And now in ‘firstManager’ script i’ve:

if (exist != true)
        {
            int spawnPointIndex = Random.Range (0, spawnPoints.Length);
        
            Instantiate (firstAid, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
        }
        exist = true;

At the end, Variable ‘exist’ is ‘true’ so condition is done.

I want change ‘exist’ variable from FirstAidCapsuleRotator. I try using

GameObject.FindWithTag("firstAidC").GetComponent<firstManager>().exist = false;

but it didn’t work.

When player raises capsule it is disappear, but i have error
‘Object reference not set to an instance of an object’ at this line:

GameObject.FindWithTag("firstAidC").GetComponent<firstManager>().exist

Sorry for my english, and noobish question :slight_smile:

All scripts come from the project ‘Nightmare Project’

Thanks

If the firstManager component is actually added to that object, then you need to make sure the tag is correct. First check to make sure GameObject.FindWithTag is actually returning something. Try this:

var firstAidObject = GameObject.FindWithTag("firstAidC");

if(firstAidObject == null)
{
     Debug.Log("First Aid Object was null");
}
else
{
    var managerComponent = firstAidObject.GetComponent<firstManager>();

    if(managerComponent == null)
   {
       Debug.Log("firstManager component does not exist on First Aid object");
   }
   else
    {
            managerComponent.exist = false;
    }

}

This will allow you to figure out which which part of your check is actually returning null.

Hm, ‘you need to make sure the tag is correct’ it’s working :stuck_out_tongue: thanks…

But still ‘exist’ at firstManager does not change…, no errors. Probably I do something wrong

//Edit it’s Work! Thanks :stuck_out_tongue:

Another question. In my empty object i have three the same scripts “first manager”. But variable ‘exist’ is changing only at the first, how can i change it? I want to change variables in all scripts. Thanks :slight_smile: