Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by malta32 · Nov 15, 2013 at 08:02 AM · gameobjectreferenceaccess

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);
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gajdot · Nov 15, 2013 at 08:25 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image malta32 · Nov 16, 2013 at 04:07 AM 0
Share

Thank you everyone for your responses.

avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image malta32 · Nov 15, 2013 at 04:03 PM 0
Share

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.

avatar image malta32 · Nov 15, 2013 at 04:46 PM 0
Share

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

avatar image tanoshimi · Nov 15, 2013 at 04:59 PM 0
Share

And what is the value of x+y*invCol? Is it less than the length of the buildings[] array?

avatar image malta32 · Nov 16, 2013 at 12:36 AM 0
Share
 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing other GameObjects Script Variables 2 Answers

Access main camera from different GameObject script 1 Answer

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

How can I access other scripts and their functions? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges