Search Unity

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

Discussion in 'Scripting' started by fullje, Oct 20, 2014.

  1. fullje

    fullje

    Joined:
    Sep 9, 2013
    Posts:
    3
    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
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class firstManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;
    7.     public GameObject firstAid;
    8.     public float spawnTime = 1f;
    9.     public Transform[] spawnPoints;
    10.     public bool exist = false;
    11.  
    12.     void Start ()
    13.     {
    14.             InvokeRepeating ("Spawn", spawnTime, spawnTime);
    15.     }
    16.     void Spawn ()
    17.     {
    18.                 if (playerHealth.currentHealth <= 0f)
    19.                 {
    20.                         return;
    21.                 }
    22.         if (exist != true)
    23.         {
    24.             int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    25.        
    26.             Instantiate (firstAid, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
    27.         }
    28.         exist = true;  
    29.     }
    30. }
    31.  
    And at 'FirstAidCapsuleRotator' ( this is a prefab ) i've:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class FirstAidCapsuleRotator : MonoBehaviour {
    6.  
    7.     GameObject player;
    8.     PlayerHealth playerHealth;
    9.     bool playerInRange;
    10.     public int howMuchHeal=20;
    11.  
    12.  
    13.  
    14.     void Awake ()
    15.     {
    16.  
    17.         player = GameObject.FindGameObjectWithTag ("Player");
    18.         playerHealth = player.GetComponent <PlayerHealth> ();
    19.     }
    20.  
    21.  
    22.  
    23.     void OnTriggerEnter (Collider other)
    24.     {
    25.         if(other.gameObject == player)
    26.         {
    27.             playerInRange = true;
    28.         }
    29.     }
    30.  
    31.     void OnTriggerExit (Collider other)
    32.     {
    33.         if(other.gameObject == player)
    34.         {
    35.             playerInRange = false;
    36.         }
    37.     }
    38.  
    39.     void Update ()
    40.     {
    41.         transform.Rotate (new Vector3 (20, 10, 0) * Time.deltaTime * 10);
    42.  
    43.         if(playerHealth.currentHealth < 100 && playerInRange)
    44.         {
    45.  
    46.             Heal ();
    47.  
    48.         }
    49.  
    50.     }
    51.  
    52.     void Heal ()
    53.     {
    54.         if(playerHealth.currentHealth < 100)
    55.         {
    56.             playerHealth.Healed(howMuchHeal);
    57.             Destroy (gameObject);
    58.             //GameObject.FindWithTag("firstAidC").GetComponent<firstManager>().exist = false;
    59.         }
    60.  
    61.     }
    62.  
    63. }
    64.  
    65.  
    66.  
    And now in 'firstManager' script i've:
    Code (csharp):
    1.  
    2. if (exist != true)
    3.         {
    4.             int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    5.        
    6.             Instantiate (firstAid, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
    7.         }
    8.         exist = true;
    9.  
    At the end, Variable 'exist' is 'true' so condition is done.

    I want change 'exist' variable from FirstAidCapsuleRotator. I try using
    Code (csharp):
    1.  
    2. GameObject.FindWithTag("firstAidC").GetComponent<firstManager>().exist = false;
    3.  
    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:
    Code (csharp):
    1.  
    2. GameObject.FindWithTag("firstAidC").GetComponent<firstManager>().exist
    3.  
    Sorry for my english, and noobish question :)

    All scripts come from the project 'Nightmare Project'

    Thanks
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    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:

    Code (csharp):
    1.  
    2. var firstAidObject = GameObject.FindWithTag("firstAidC");
    3.  
    4. if(firstAidObject == null)
    5. {
    6.      Debug.Log("First Aid Object was null");
    7. }
    8. else
    9. {
    10.     var managerComponent = firstAidObject.GetComponent<firstManager>();
    11.  
    12.     if(managerComponent == null)
    13.    {
    14.        Debug.Log("firstManager component does not exist on First Aid object");
    15.    }
    16.    else
    17.     {
    18.             managerComponent.exist = false;
    19.     }
    20.  
    21. }
    22.  
    This will allow you to figure out which which part of your check is actually returning null.
     
  3. fullje

    fullje

    Joined:
    Sep 9, 2013
    Posts:
    3
    Hm, 'you need to make sure the tag is correct' it's working :p thanks...

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

    //Edit it's Work! Thanks :p

    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 :)
     
    Last edited: Oct 20, 2014