Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 dansav · Oct 05, 2011 at 09:03 PM · variablesaccessfunctionsother

problems accessing variables and functions in another script

I have two scripts main and joints.

I want to access functions and variables in joints from main.

I have a gameObject named MainObject which both scripts are attached to.

This is not working so far.

joints:

var myarray=new Array();  
myarray[0]="test";

main:

var myjoints:joints;  
myjoints=GameObject.Find('MainObject').GetComponent(joints);  
Debug.Log(myjoints);//returns MainObject (joints);  
Debug.Log(myjoints.myarray[0]);//returns out of index.
It seems like it finds the joints script but not any variables attached to the script. Can anyone explain why this is not working?

thanks,

Dan

Comment
Add comment · Show 2
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 dansav · Oct 06, 2011 at 12:02 AM 0
Share

It doesn't seem to make any difference how I reference the gameobject. It still gives the same result. It finds the joints script --or so it says, but can't find the variable in the joints script.

avatar image dansav · Oct 06, 2011 at 12:18 AM 0
Share

It works fine if I add yield WaitForSeconds(5); before checking the array. So it's some ti$$anonymous$$g issue, but what?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Tasarran · Oct 05, 2011 at 09:37 PM

This has been answered before, but here goes again...

(BTW, please use code tags to define your code, it's the button with the 1's and 0's on it)

Your MAIN should get the connection to JOINTS this way:

 var myjoints;
 myjoints=gameObject.GetComponent(joints);

GameObject (with capital G is different than gameObject. Lowercase g means the gameobject this script is attached to. Only use capital G to refer to the GameObject type. Also, there was no need to do the Find(), just use GetComponent. You only need to do the Find when you are looking outside the current GameObject...

Comment
Add comment · Show 8 · 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 Steven-Walker · Oct 06, 2011 at 12:16 AM 1
Share

I would suggest making the types explicit for js code: var myjoints : joints = gameObject.GetComponent(typeof(joints)) as joints;

And if myjoints.myarray[0] is returning out of index, then that simply means your array is uninitialized. So you need to make sure that your first script is testing the value of myarray only after it has been set to something.

avatar image aldonaletto · Oct 06, 2011 at 12:34 AM 0
Share

@Steven Walker is right: maybe main is running before the initialization of myarray. You can assign the value to myarray in joints' Awake, and read it in main's Start or Update.

avatar image Steven-Walker · Oct 06, 2011 at 12:39 AM 0
Share

I often get around this by adding a variable to script like: var isSetup : boolean = false;

function Start() {

// do intialization stuff

isSetup = true; }

Then in your other script you can check:

while(!myjoints.isSetup) yield;

This will force main script to wait until the other is ready. NOTE: You will likely need to do the yield in another method such as:

StartCoroutine("DoSetup")

avatar image dansav · Oct 06, 2011 at 12:47 AM 0
Share

I need to learn more about what code loads first since there are two scripts attached to the same object. Is there a way to force the order to be correct. If I use Awake() in one and Start() in the other how will I know which one fires first. I guess I can test with debugs???

avatar image dansav · Oct 06, 2011 at 02:38 AM 0
Share

Steven: That was a good trick and it worked. Thanks.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Accessing function from another script won't work 1 Answer

Get GameObject name from other object. 2 Answers

Accessing classes class from other script 2 Answers

Access others functions C# 2 Answers

Access other script from Editor script 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