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 GreenMatrix · Feb 07, 2013 at 09:21 AM · objectgetcomponentaccess

Howto access a variable in another script?

In level 1 I have an object (obj1) with a script (script1), and a variable (var1). I have attached:

 function Awake () {DontDestroyOnLoad (transform.gameObject);}

to script1 to keep it alive when level 2 is loaded. In level 2 i have an object (obj2) that would need to check the variable (var1) in obj1 - script1.

I have tried on obj2:

 function Start(){
 var checkit : script1 = GetComponent(script1).var1;
 Debug.Log(checkit);}

but I think this might only search for the code on the same object, not sure. Could anyone help with how to access the other object, please?

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 CodeMasterMike · Feb 07, 2013 at 09:27 AM

In order to get the objects variable, you need to access the component (your case, the script) through the gameobject, something like this:

 var other : script1 = obj1.GetComponent(script1);
 var variableFromScript = other.var1;

I only use C# so the code might not be right, but hopefully you see the thread of thought here.

Good luck!

Comment
Add comment · Show 3 · 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 GreenMatrix · Feb 07, 2013 at 10:12 AM 0
Share

I changed to the suggestion, but it continues to give errors. It says: BCE0005: $$anonymous$$ identifier: ´obj1´. Still - Thx for suggestion!

avatar image CodeMasterMike · Feb 07, 2013 at 10:24 AM 0
Share

You cant just write 'obj1', it needs to be a valid gameobject before you can try and access the component.

Find the object you want to access (in your case, the 'obj1' gameobject) with a search function like:

G

ameObject.FindWithTag

or,

GameObject.Find

or attach it to the script by settings a public Gameobject in the class.

avatar image GreenMatrix · Feb 07, 2013 at 10:46 AM 0
Share

THX! Now it seems to work!

 function Start(){
 var checkit : script1 = GameObject.Find("obj1").GetComponent(script1);
 var variableFromScript = checkit.var1;
 }

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

10 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

Related Questions

Methods of Accessing scripts - performance & neatness 1 Answer

NullReferenceException: Object reference not set to an instance of an object ..... 1 Answer

GetComponent is not member of Object? 2 Answers

problem of switching character in the game 0 Answers

how do you access booleans from another script 1 Answer


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