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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with referencing another script within a script

Discussion in 'Scripting' started by DoubleDonjon, Dec 25, 2015.

  1. DoubleDonjon

    DoubleDonjon

    Joined:
    Feb 10, 2015
    Posts:
    9
    Hi, I'm developing a game with Unity and I'm having a huge game breaking problem which I just can't fix. I'm trying to reference a variable(boolean) in a script called "NetworkMaster" but it keeps coming out as null:

    Code (CSharp):
    1.    
    2.     bool myIsLoadingDone;
    3.  
    4. void Start () {
    5.     NetworkMaster nm = (NetworkMaster)this.transform.GetComponent ("NetworkMaster");
    6.  
    7.     if (nm != null) {
    8.         StartCoroutine ("Wait");
    9.  
    10.         myIsLoadingDone = true;
    11.         nm.isLoadingDone = myIsLoadingDone;
    12.     }
    13. }
    14.  
    15. IEnumerator Wait() {
    16.         yield return new WaitForSeconds (2.5f);
    17.     }
    18.  

    "nm.isLoadingDone" is the boolean that I'm trying to change to "myIsLoadingDone"
     
  2. BrienKing

    BrienKing

    Joined:
    Oct 11, 2015
    Posts:
    35
    Is your script attached to an object in the scene?

    If it is, you should get a reference to that object then do something like this:

    Code (CSharp):
    1.             newButton = GameObject.Instantiate(GameItemPrefab) as GameObject;
    2.             tempScript = newButton.GetComponent<GameItemScript>();
    3.  
    newButton is of type GameObject. GameItemPrefab is a prefab for a component that I am using, but you substitute that line for any game object in your scene.

    tempScript is the type GameItemScript.

    The important lines is the second line that looks for the script called "GameItemScript" attached to the GameObject "newButton".
     
  3. DoubleDonjon

    DoubleDonjon

    Joined:
    Feb 10, 2015
    Posts:
    9
    Yes it is attached to an object, but the object is always in the scene, I don't see why I need to instantiate it. What I'm trying to accomplish is because for some reason I couldn't put an IEnumerator function in the NetworkMaster script, and I was trying to create a loading period at the start of the scene, so I put it in this script and I was trying to reference it through saying "nm.isLoadingDone" but it says "Object reference not set to an instance of an Object". The odd thing is the reference line(Line 5) is completely correct.
     
    Last edited: Dec 25, 2015
  4. BrienKing

    BrienKing

    Joined:
    Oct 11, 2015
    Posts:
    35
    Try this:

    Code (CSharp):
    1.     NetworkMaster nm = (NetworkMaster)GetComponent <NetworkMaster>();
     
  5. DoubleDonjon

    DoubleDonjon

    Joined:
    Feb 10, 2015
    Posts:
    9
    Dang, didn't work. Nice try
     
  6. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,245
    Is it attached to the same object as the script you are using?

    If not then you have to find the object before using the getComponent?

    Also once you have the variable why don't you just nm.isLoadingDone rather than place in another variable?

    I included the code you need to use:

    Code (CSharp):
    1. GameObject.Find("ObjectName").GetComponent<NetworkMaster>();
     
  7. DoubleDonjon

    DoubleDonjon

    Joined:
    Feb 10, 2015
    Posts:
    9

    Yes, "Network Master" and "Loading" script are on the same empty in the scene.
     
  8. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,245
    then this example will work, you don't need the (NetworkMaster) part as you are already putting it into a NetworkMaster

    If it isn't then you need to look at the objects in your scene.

    Are both of the scripts on the same Object in your empty scene?
     
  9. DoubleDonjon

    DoubleDonjon

    Joined:
    Feb 10, 2015
    Posts:
    9
    Like I said before, they are, I've tried using a public variable and any other method that I could think of. I tried your aforementioned method and it still returned as null. Do you know any other methods for referencing a variable in another script?
     
  10. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,245
    make your network variable public at the top and then see if you can drag the object with the NetworkMaster into it. Just to make sure your type is right.

    Then work your way up to calling it in the script.

    If that works keep it as a public var so you see it in the editor and run again to see if it gets filled.

    Saying "it doesn't work" and giving us not much doesn't help. The code works fine because I use that a lot. So we need to look at your scene to figure what it is wrong.