Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Can't access a variable from another script

Discussion in 'Scripting' started by Sir_Mister_Lord_Emperor, Apr 29, 2018.

  1. Sir_Mister_Lord_Emperor

    Sir_Mister_Lord_Emperor

    Joined:
    Feb 17, 2018
    Posts:
    8
    Here is the script that houses the variable I need. For more general info scroll to the bottom where I explain what the whole thing should do.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class TimerIniciative : MonoBehaviour {
    8.  
    9. // Iniciative //
    10.     Image Blue;
    11.  
    12.     public bool StartOfTurn = false;
    13.     private float MaxTimeI =6f;
    14.     private float MTimeI =6f;
    15.     private float timeLeftI =6f;
    16.  
    17.  
    18.  
    19.     // START OF PART I NEED
    20.     public void Start () {
    21.         if (StartOfTurn == true) { // this variable is set true in another script
    22.             gameObject.SetActive (false);
    23.         } else {
    24.        
    25.             gameObject.SetActive (true);}
    26.        
    27.         Blue = GetComponent<Image> ();
    28.  
    29.         timeLeftI = MTimeI;
    30.  
    31.     }
    32.  
    33.     // END OF PART I NEED
    34.    
    35. // TrurnCounter ; Turn; Trurn Counter;
    36.  
    37.     // Update is called once per frame
    38.     void Update () {
    39.  
    40.  
    41.  
    42.         if (timeLeftI > 0) {
    43.             //StartOfTurn = true;
    44.            
    45.  
    46.            
    47.             timeLeftI -= Time.deltaTime;
    48.             Blue.fillAmount = timeLeftI / MaxTimeI;
    49.         } else if (timeLeftI <= 0) {
    50.            
    51.  
    52.  
    53.    
    54.             //Debug.Log("End of  turn");
    55.             //Time.timeScale = 0;
    56.  
    57.  
    58.             Reset ();
    59.             //TurnCounter.display(); research this later
    60.         }
    61.  
    62.    
    63.  
    64.     }
    65.    
    66.         void Reset() {
    67.         MaxTimeI =6f;
    68.         MTimeI =6f;
    69.         timeLeftI =6F;
    70.         Blue.fillAmount = MaxTimeI;
    71.    
    72.  
    73.     }
    74.  
    75. }
    76.  
    77. // End of INICIATIVE  //
    78.  



    And here is the code that is supposed to use it:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class BattleStateManager : MonoBehaviour {
    8.  
    9.  
    10.  
    11.  
    12.  
    13.  
    14.     public TurnState curentState;
    15.     public enum TurnState
    16.     {
    17.         WAITING, // put the round couter here
    18.         ADDAttacker, // put the iniciative here
    19.         ATTACKING,
    20.         dead,
    21.         DEFENDING,
    22.     }
    23.  
    24.  
    25.  
    26.  
    27.     // Use this for initialization
    28.     void Start () {
    29.  
    30.  
    31.        
    32.     }
    33.  
    34.  
    35.     GameObject[] IniciativeMeter; // here are stored all the objects with the iniciative tag
    36.     TimerIniciative iniciative;
    37.  
    38.  
    39.     //GameObject Chase;
    40.     //Chase StartCombat;
    41.    
    42.     // Update is called once per frame
    43.     void Update () {
    44.  
    45.  
    46.         switch (curentState) {
    47.  
    48.         case (TurnState.WAITING):
    49.  
    50.             //if (GameObject.FindGameObjectsWithTag ("Iniciative")) {
    51.                 //Debug.Log ("roger roger");
    52.             //}
    53.  
    54.             break;
    55.  
    56.  
    57. // that is where I call it
    58.  
    59.         case (TurnState.ADDAttacker):
    60.             IniciativeMeter = GameObject.FindGameObjectsWithTag ("Iniciative");
    61.             foreach (GameObject r in IniciativeMeter) {
    62.                 iniciative = gameObject.GetComponent<TimerIniciative>();
    63.                  bool StartOfTurn = true;
    64.                 //gameObject.SetActive (true);
    65.                 Debug.Log ("roger roger");
    66.             }
    67.             break;
    68.  
    69. // ------------------------------------------------------
    70.  
    71.         case(TurnState.ATTACKING): // if your iniciative reaches 0
    72.             break;
    73.  
    74.         case(TurnState.dead):
    75.             break;
    76.  
    77.         case(TurnState.DEFENDING): // if a player who is attacker damages you
    78.  
    79.             break;
    80.  
    81.         }
    82.     }
    83.  
    84.  
    85. }
    86.  
    87.  
    What should the code do ?
    The two code piece are a part of a turn based game that has rounds, where participants have their iniciative decide when they will act. The first piece of code calls the iniciative bar and the second contains all the states that trigger different stages. Since the iniciative bar is otherwise inactive, I want to use the second script to enable it by accessing the BOOL variable of the Iniciative script (1st script) and set it to true, thus enabling it. I've made a mistake, but I don't know what and where it is.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    You already have the iniciative script referenced, you need to do:
    iniciative.StartOfTurn = true;
     
  3. Sir_Mister_Lord_Emperor

    Sir_Mister_Lord_Emperor

    Joined:
    Feb 17, 2018
    Posts:
    8
    Hmm it gave me null reference error. I tried restarting it a couple of times, since monoDevelop sometimes feels charitable and hands out null reference errors.that disappear after a restart. The first time i replaced line 63
    Code (CSharp):
    1. bool StartOfTurn = true;
    because I thought the mistake was in how I structured it, but putting it alongside in the foreach doesn't work either.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    It seems like you're new to programming. This is not a good idea. You should really look into C# before you start using Unity, so for example, you know, what a nullreference error is. Do you have the two scripts on the same object?
     
  5. Sir_Mister_Lord_Emperor

    Sir_Mister_Lord_Emperor

    Joined:
    Feb 17, 2018
    Posts:
    8
    No they are not on the same object. The Iniciative script is on a Filled Image and the Battle State manager is on a cube.
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Yeah, well, what you're doing, is saying to unity, that he should take the object the second script is on (gameObject. part)
    look for a script of the type TimerIniciative (GetComponent) and save it to a variable. But, because you don't have that kind of script on that object, it won't find anything, thus returning null. MonoDevelop won't ever give you a nullreference error. What you need to do, is create a public GameObect variable, assign the object with the TimeIniciative script on it to this variable and after that you can get that script by changing "gameObject" on line 62 to your variable

    Edit:
    Or you can skip the GetComponent step, and assign the first script itself to your second script
     
    Last edited: Apr 29, 2018
  7. Mokzen

    Mokzen

    Joined:
    Oct 10, 2016
    Posts:
    91
    As gorbit99 says, you're telling Unity to look for a script called "TimerIniciative" on the same gameobject as your "BattleStateManager" script is located. If Unity cannot find "TimerIniciative" on the object it's looking at, it throws a Null Reference Exception warning.

    Also, i'd advice you to locate all your global variables in the beginning of your scripts. It just makes it easier to read.
     
  8. Sir_Mister_Lord_Emperor

    Sir_Mister_Lord_Emperor

    Joined:
    Feb 17, 2018
    Posts:
    8
    Thanks, it worked this time. I will probably go back and start off with more basic games, because the idea I am trying to tackle is too complex for a newb.
     
  9. Mokzen

    Mokzen

    Joined:
    Oct 10, 2016
    Posts:
    91
    Common problem in indie development ;) Don't let it get to you.