Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Calling a variable from another script?

Discussion in 'Scripting' started by LukeDodds, Nov 7, 2014.

  1. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    Setting up a variable in my gamecontroller
    an int called LevelNumber

    For a test i want it to print to log when it collides with a trigger in another script, iv tried doing this but coming up with a few errors, i feel like im on the right track but cant get it working please help
    GameControllerScript:
    Code (csharp):
    1.  
    2. public int LevelNumber = 0;
    3. void Start () {
    4.         LevelNumber = 1;
    5.     }
    6.  
    The test trigger script:
    Code (csharp):
    1.  
    2. void OnTriggerEnter( Collider other )
    3.     {
    4.         GameObject go = other.gameObject;
    5.         GameControllerScript.GetComponent (GameControllerScript.LevelNumber);
    6.         Debug.Log (GameControllerScript.LevelNumber);
    7. }
    8.  
     
    Last edited: Nov 8, 2014
  2. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    Plz use Code Tags Like this

    Code (csharp):
    1.  
    2. Public John john;
    3.  
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    When u type u hv a option to insert code just past it there it seems like a page with lanes
     
  4. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    [code.]
    *Code HERE*
    .[/code]
    Take away the .
     
  5. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    Yeah thanks got it sorted now as you can see,
    anyone help with the issue?
     
  6. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    How i do it is have GameController be a parent or child of test collider
    Then do this

    Code (csharp):
    1.  
    2. public GameController game;
    3.  
    4. void Update(){
    5.     Debug.Log(game.LevelNumber);
    6. }
    7.  
     
    LukeDodds likes this.
  7. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
    Luke, try this...

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3. GameObject go = other.gameObject;
    4. int level = go.GetComponent<GameControllerScript>().LevelNumber;
    5. Debug.Log (level);
    6. }
     
    LukeDodds likes this.
  8. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    I think Luke needs to read a bit on Object Oriented Programming, as well as basic C# and programming lessons.

    Do not take this the wrong way, I'm not saying that you are stupid, or anything like this.
    What I mean is that this is the sort of mistakes that anyone that does not understand the language could do, and even if learning on it takes time, it give you the opportunity to do these things and read the error messages in order not to be stuck waiting from an answer from other people, that will always seem to take a long time, because you are stuck on it at the time.

    I do pass as a very mean person picking on others, when I'm writing these things...
     
  9. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    Hey there thanks for the helpful idea but it comes up with an error that says
    Code (csharp):
    1.  
    2.  
    3. NullReferenceException: Object reference not set to an instance of an object
    4. Key.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Key.cs:20)
    5.  
    6.  
     
  10. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    I understand with what your saying and completely agree i just need abit of help right now for a push
     
  11. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    Also tried this but comes up with the same error as what i got for grizzly
     
  12. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
    The other object must have your script attached. You can handle this like so...
    Code (CSharp):
    1.  
    2. GameObject go = other.gameObject;
    3. GameControllerScript script = go.GetComponent<GameControllerScript>();
    4.  
    5. if (script != null) {
    6.    Debug.Log (script.LevelNumber);
    7. } else {
    8.    Debug.Log ("Script not found!");
    9. }
    10.  
    11.  
     
  13. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    Hey getting no errors now but its coming up with the "Script not found!"
     
    Last edited: Nov 8, 2014
  14. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
    "Script not found!" will appear when you enter a trigger that does not contain your script. That message was included for debugging only and can(should) be removed when you're done.

    Since you're setting LevelNumber to 1 in Start(), make sure your scripts are enabled (ticked on the component) and Start() is correctly being called.
     
  15. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    When you say the trigger doesnt contain my script, should i add the GameController script to the key object then?
    i already have the gamecontroller parenting all other objects so i thought that would be alright
     
  16. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
    All objects requiring this functionality should have your script attached, yes.
     
  17. LukeDodds

    LukeDodds

    Joined:
    Oct 24, 2014
    Posts:
    46
    So i should be attaching the gamecontroller script to all relevant objects not just an empty object,
    or adding the key script to the gamecontroller?
     
  18. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
    LukeDodds likes this.