Search Unity

Error when trying to modify variable in Script A with Script B

Discussion in 'Scripting' started by MarkVatani, Mar 2, 2015.

  1. MarkVatani

    MarkVatani

    Joined:
    Sep 4, 2012
    Posts:
    39
    I have two menus. One is a main menu that opens a second, yet still stays active in case the player clicks cancel on the second menu. I simply hide the first menu by changing it's overall width from 100 to 0 when the second is opened. When I click the close button on the second menu, it's supposed to change the 0 back to 100 on the first, revealing it again, and deactivates the second menu.
    The problem is, I get a "Object reference not set to an instance of an object" error on line 18 of my GUIInformational script when clicking the 2nd menu close button even though I thought I have the code right and the first menu object placed in the prefab spot in the Inspector. Here is my code for both scripts:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RightClickMenu : MonoBehaviour {
    5.  
    6.     public RightClickMenu myRightClickMenu;
    7.     public int initialWidth = 100;
    8.  
    9.     public GameObject prefab;
    10.  
    11.     void OnGUI() {
    12.  
    13.         GUI.BeginGroup(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 100, initialWidth, 50));
    14.         GUI.Box(new Rect(0, 0, 100, 50), "Menu");
    15.  
    16.         if (GUI.Button(new Rect(5, 5, 16, 16), new GUIContent("")))
    17.             {
    18.                 Debug.Log("Menu X Button clicked");
    19.                 gameObject.SetActive(false);
    20.             }
    21.  
    22.         if (GUI.Button(new Rect(0, 25, 100, 25), "New Menu"))
    23.             {
    24.                 Debug.Log("Clicked the New Menu button");
    25.                 prefab.SetActive(true);
    26.                 initialWidth = 0;
    27.             }
    28.         GUI.EndGroup();
    29.     }
    30. }
    Second Script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUIInformational : MonoBehaviour {
    5.  
    6.     public RightClickMenu myRightClickMenu;
    7.     public int initialWidth = 100;
    8.     public GameObject parentPrefab;
    9.  
    10.     void OnGUI() {
    11.  
    12.         GUI.BeginGroup(new Rect(Screen.width / 2 - 235, Screen.height / 2 - 235, 470, 390));
    13.  
    14.         GUI.Box(new Rect(0, 0, 470, 390), "New Menu");
    15.         if (GUI.Button(new Rect(10, 13, 60, 16), "Close"))
    16.         {
    17.             Debug.Log("Close Button clicked");
    18.             myRightClickMenu.initialWidth = 100;
    19.             Debug.Log("RightClick set to 100");
    20.             parentPrefab.SetActive(false);
    21.             Debug.Log("Parent prefab turned off");
    22.             gameObject.SetActive(false);
    23.             Debug.Log("This object turned off");
    24.         }
    25.  
    26.         GUI.EndGroup();
    27.     }
    28. }
    What am I doing wrong?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    The problem is that you have to reference the gamobject in the scene and not the prefab that is in your project.
    You can instantiate prefabs , then they become normal gamobjects that live in your scene but before that you cant access them.
     
  3. MarkVatani

    MarkVatani

    Joined:
    Sep 4, 2012
    Posts:
    39
    Both scripts are attached to gameobjects in the scene. I even had the the GUIInformational script/gameobject parented to the RightClickMenu script/gameobject and got the same error.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I don't see anything in your code that would cause the problem you describe, so my only guess is that you've somehow made a mistake in the inspector. Maybe instead of setting the myRightClickMenu field of the GUIInformational, you accidentally set the myRightClickMenu field of the RightClickMenu? (Why does the RightClickMenu even have that field? It doesn't appear to be used for anything in the code you posted.)