- Home /
Referencing a Public GameObject from another script.
I have two objects, TileSelect and GameMenu with scripts TileSelect and GameMenu attached respectively.
Different gameobject are fed into SetBuilding depending on which GUI button was pressed.
public class GameMenu : MonoBehaviour
{
public GameObject curBuilding;
//GUI Menu Script
{
SetBuilding(buildings[x+y*invCol]);
}
public void SetBuilding(GameObject b)
{
Debug.Log(b.name);
curBuilding = b;
Debug.Log(curBuilding);
}
}
curBuilding updates in the inspector whenever I click a new GUI button. Whenever I try and reference curBuilding in Tile I always get Null. I have a feeling I'm missing a reference or a .transform but I can't figure it out.
public class Tile : MonoBehaviour
{
public GameObject mapTile;
public GameMenu gameMenu;
public void OnMouseDown()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log("Right Mouse");
Debug.Log(gameMenu.curBuilding);
//Instantiate(gameMenu.curBuilding.transform, transform.position, Quaternion.identity);
Did you link game$$anonymous$$enu to the script in the inspector? Because that could lead to null variable. Click on your object containing the tile script and drag your object containing the Game$$anonymous$$enu class in the slot for "Game $$anonymous$$enu" field.
Answer by malta32 · Nov 16, 2013 at 04:25 AM
Solved it. Trying to modify the variable from the GameMenu only changes the prefab GameObject and not the instansiated clones. Referencing the GameMenu from the cloned GameObjects TileSelect Script works.
public void OnMouseDown()
{
if(Input.GetMouseButtonDown(0))
{
GameObject gm = GameObject.FindWithTag("GameMenu");
tileBuilding = gm.GetComponent<GameMenu>().curBuilding;
Instantiate(tileBuilding, transform.position, Quaternion.identity);
Answer by tanoshimi · Nov 15, 2013 at 08:24 AM
In the GameMenu code above, you're never calling SetBuilding(buildings[x+y*invCol]); so it's not surprising that curBuilding is always null - it never gets assigned a value.
Try initialising curBuilding to a non-null value (e.g. in Start() of GameMenu) and check that you can retrieve that value from Tile.
p.s. you'll find it easier to see where your code is failing if you make your Debug.Logs more descriptive. e.g.
public void SetBuilding(GameObject b)
{
...
Debug.Log("Value of curBuilding in SetBuilding() is: " + curBuilding);
...
}
Otherwise you're just printing the name of the building lots of times but can't tell why.
Thank you very much for the response. I'm new to Unity. I'm going to try this later when I get out of work.
I tried initializing curBuilding in the Start(). curBuilding = buildings[invCol]; So it was assigned a non null value. I still could not call the curBuilding from Game$$anonymous$$enu. I set up a couple other variables in the script and was able to reference them with no problem. It's just curBuilding that I can't seem to pass between the two.
These two debugs are in the Tile script. I can get the length of the array but not the curBuilding GameObject.
Debug.Log("Get total building in Array from Game$$anonymous$$enu: " + game$$anonymous$$enu.buildings.Length);
Debug.Log("Get current building from Game$$anonymous$$enu: " + game$$anonymous$$enu.curBuilding);
I'm not sure if this is what you mean but I'm calling SetBuilding(buildings[x+y*invCol]); everytime a GUI button is pushed
And what is the value of x+y*invCol
? Is it less than the length of the buildings[] array?
private int invRow = 4;
private int invCol = 2;
void OnGUI()
{
if(display$$anonymous$$enu)
{
GUI.BeginGroup(new Rect((Screen.width - menuWidth) - menuOffSet, menuOffSet, menuWidth, menuHeight), "");
GUI.Box(new Rect(0, 0, menuWidth, menuHeight), "");
for (int y = 0; y < invRow; y++)
{
for (int x = 0; x < invCol; x++)
{
buttonName = (x + y * invCol); //TODO: shorten me/remove #
if(GUI.Button(new Rect(((buttonSpacing + buttonWidth) * x) + buttonEdgeOffSet + 15, ((buttonSpacing + buttonHeight) * y) + buttonEdgeOffSet + 150, buttonWidth, buttonHeight), buildings[x+y*invCol].name))
{
SetBuilding(buildings[x+y*invCol]);
}
}
}
GUI.EndGroup();
8 buildings in the array, 8 buttons, 1 for each building.
Your answer
Follow this Question
Related Questions
Accessing other GameObjects Script Variables 2 Answers
Access main camera from different GameObject script 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to run function in another script with prefabs? C# 2 Answers
How do you reference a GameObject in a C# script that is a part of that GameObject? 1 Answer