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 U2-84 · Oct 29, 2021 at 03:22 PM · gameobjectreferencepassinganother script

Problem passing a GameObject to another script

Hello all,
I have the following scenario:
ScriptA is attached to Object "A" and it's in charge of storing all the objects with a certain tag:

 public List<GameObject> items;
 private GameObject[] startingItems;
     
 void Start()
 {
    items = new List<GameObject>();
    startingItems = GameObject.FindGameObjectsWithTag("test");
 
    foreach (GameObject element in startingItems)
    {
       items.Add(element);
    }
 }

Then I have ScriptB attached to Object "B": it gets the list from ScriptA and instantiates a UI Panel (prefab) for each element of the list. The instantiated panel has the screen coordinates of the element:

 public GameObject ui_panelPrefab; //I'll drag this
 private ScriptA scriptA;
 
 void Start()
 {
    scriptA = GameObject.Find("A").GetComponent<ScriptA>();
 
    foreach (GameObject element in scriptA.items)
    {
       PanelInst(element);
    }
 }
 
 void PanelInst(GameObject obj)
 {
    Vector3 screenPosition = Camera.main.WorldToScreenPoint(obj.transform.position);
    GameObject ui_panel = Instantiate(ui_panelPrefab, screenPosition, ui_panelPrefab.transform.rotation);
    ui_panel.GetComponent<ScriptC>().screenPosition = screenPosition;
 }
  

Now let's move to the instantiated panel. It has the following script attached that simply move the panel (changing its screen coordinates) as the "targetObject" moves:

 public GameObject targetObj;
 public Vector3 screenPosition;
 
 void Update()
 {
    screenPosition = Camera.main.WorldToScreenPoint(targetObj.transform.position);
    transform.position = screenPosition;
 }

Now I need the "targetObj" above to be exactly the "obj" of the ScriptB. I did the dumb move and tried to add this line to the PanelInst() function of ScriptB:

 ui_panel.GetComponent<ScriptC>().targetObj= obj;

...but it obviously doesn't work as intended: it passes only certain parameters of the "obj" gameobject (its name and starting position) but not the object itself, so the panel position in the last script never updates no matter if the "obj" moves (because "obj" in the second script and "targetObj" in the third script are not the same object).
How can I solve this problem? I can't try different setups (like the UI panel as child of the target object): I have to keep them separate but yet I need the UI panel to follow the screen coordinates of the target object.

Thanks in advance for your help and I'm sorry for the weird formatting of the code. I'm at work, I can't use Unity or code editors, I'm just typing here and I probably did mistakes or forgot things here and there. Just be aware that the code syntax is correct in Unity: it's just not behaving correctly.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

194 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Reference variable in other script 1 Answer

GameObject reference breaks 1 Answer

Copying GameObjects with reference types 1 Answer

Null Reference at (wrapper managed-to-native) 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 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