Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NickWalker12 · Mar 13, 2012 at 11:42 AM · c#variablefunctionreferenceaccessor

C'# When Taking Variables From Other Scripts, The Values Do Not Update

Hi, cheers for reading;

-Two classes: Map_Mech & Map_Tile_Generic

-There is one object that contains the script Map_Mech, that object is called pMap_Mech (prefab)

-There are potentially hundreds of clones of the prefab (pTile) that contains the Map_Tile_Generic script. I want the Map_Tile_Generic script to take a value from the single Map_Mech, and use it (accessor functions). I have drag/dropped the Map_Mech prefab into the script area in the pTile prefab, and it finds it (`other.getTileScale()`) sets to the correct value, as its only assigned once, on create)

Map_Mech:


 public Vector3 GetNewVector () { return vNewVector; }

"Map_Tile_Generic"


 private GameObject oBase;
     
     public Map_Mech other;
     
     void Awake()
     {        
         oBase = GameObject.CreatePrimitive (PrimitiveType.Cube);
         oBase.transform.position = other.vNewVector;
         oBase.transform.localScale = other.GetTileScale(); 
     }    

Map_Mech changes the value of vNewVector in real time, however, when I use the function, it always returns Vector3(0,0,0); I managed to solve my previous question after posting, so I'm hoping I can do the same XD However, if anyone does know (or has an idea), the info would be fantastic! I'm online every day between 10am-10pm GMT (usually). Ta

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

1 Reply

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

Answer by aldonaletto · Mar 13, 2012 at 12:18 PM

If I understood correctly, other is a reference to the prefab Map_Mech - but it should be a reference to the actual instance of Map_Mech that exists in your scene. Instead of assigning other in the Editor, you should find it at runtime, and in Start, not Awake (Awake is called during object creation, and other objects may not exist yet):

public Map_Mech other;

 void Start()
 {  
    other = GameObject.Find("Map_Mech"); // find Map_Mech instance by name
    oBase = GameObject.CreatePrimitive (PrimitiveType.Cube);
    oBase.transform.position = other.vNewVector;
    oBase.transform.localScale = other.GetTileScale(); 
 }   

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 NickWalker12 · Mar 13, 2012 at 01:00 PM 0
Share

Thanks for the reply! Questions are numbered:

  1. Put code in, but I get the same error that I got while trying to get it to work previously: "Cannot implicitly convert 'UnityEngine.GameObject' to '$$anonymous$$ap_$$anonymous$$ech'. Changing the private $$anonymous$$ap_$$anonymous$$ech other; to private GameObject other also returns two errors, because now I cannot access GetNewVector() or GetTileScale(). Do I need to use GetComponent? That's what other use, but I keep getting errors about finding specific instances. The only $$anonymous$$ap_$$anonymous$$ech find I can use is FindObjectofType(), which I dont know how to use.

  2. Is the removal of the accessor function intentional in your reply? If so, why so? :)

Once again, cheers!

avatar image aldonaletto · Mar 13, 2012 at 08:53 PM 0
Share

1- $$anonymous$$y fault! GameObject.Find finds an instance by name and returns its GameObject reference, but other is a $$anonymous$$ap_$$anonymous$$ech variable, which I suppose is a reference to a script $$anonymous$$ap_$$anonymous$$ech.cs. Supposing $$anonymous$$ap_$$anonymous$$ech.cs is attached to the object named "$$anonymous$$ap$$anonymous$$ech", you should do the following:

   other = GameObject.Find("$$anonymous$$ap$$anonymous$$ech").GetComponent< $$anonymous$$ap_$$anonymous$$ech>();
   oBase = ...

2- I only copy/pasted the relevant lines, thus the oBase declaration was not included in the answer - but you will need it, for sure!

avatar image NickWalker12 · Mar 13, 2012 at 09:43 PM 0
Share
  1. Unfortunately, again, it doesn't work. Same thing as happened at the start. This code compiles fine:

private GameObject oBase; public GameObject other; void Start(){
other = GameObject.FindGameObjectWithTag("p$$anonymous$$ap_$$anonymous$$ech").GetComponent<$$anonymous$$ap_$$anonymous$$ech>(); oBase = GameObject.CreatePrimitive (PrimitiveType.Cube); oBase.transform.localPosition = other.vNewVector; oBase.transform.localScale = other.GetTileScale(); }

However, in debug, "other" returns null. Interestingly, when I use this: "other = GameObject.FindGameObjectWithTag("p$$anonymous$$ap_$$anonymous$$ech")/.GetComponent<$$anonymous$$ap_$$anonymous$$ech>()/;" OR "other = GameObject.FindGameObjectWithTag("p$$anonymous$$ap_$$anonymous$$ech");" it finds the exact gameobject. The get component just does not seem to update.

I am lost.

avatar image aldonaletto · Mar 15, 2012 at 12:53 PM 0
Share

The correct syntax is GameObject.FindWithTag (don't know if FindGameObjectWithTag works too). Fix it, and try to get the p$$anonymous$$ap_$$anonymous$$ech tagged object first, then get the component $$anonymous$$ap_$$anonymous$$ech:

void Start()
    {
       // find $$anonymous$$ap_$$anonymous$$ech instance by tag
       GameObject map = GameObject.FindWithTag("p$$anonymous$$ap_$$anonymous$$ech");
       other =  map.GetComponent< $$anonymous$$ap_$$anonymous$$ech>(); // get $$anonymous$$ap_$$anonymous$$ech
       oBase = GameObject.CreatePrimitive (PrimitiveType.Cube);
       oBase.transform.position = other.vNewVector;
       oBase.transform.localScale = other.GetTileScale(); 
    }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to reference another script and call a function in C# ? 2 Answers

Multiple Cars not working 1 Answer

When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this? 3 Answers

Distribute terrain in zones 3 Answers

Can I use 'variable name & function name' in a foreign language? (유니티는 '한글 변수명 & 한글 함수명' 이름 지정이 가능한가요?) 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