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 stick_penguin · Aug 17, 2013 at 01:12 PM · gameobjectvariablescriptingbasics

Accessing Variables within another gameobject's script

So far I have two game objects, one which has to change one of the variables in the first one's script. So far, I have this,

 private var firstObject : GameObject;
 
 function Start () {
     firstObject = GameObject.Find("yellowBarrel");
     firstObject.RollingLogic.values = 4;

}

and in the other script...

 public var values: int;
 [...]

(The script containing the variable is called RollingLogic) I know the yellow barrel was found because I can apply a transformation to it, I just can't figure out how to access it's variables. Thanks for any help!

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

2 Replies

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

Answer by JoaquinRD · Aug 17, 2013 at 01:21 PM

The problem is that your firstObject variable is defined as a GameObject. In order to access the values in your RollingLogic script, you need to store that instance of the script in a variable of type RollingLogic. To do this, first find the GameObject, then get the RollingLogic Component from that GameObject. For example:

 private var firstObject : RollingLogic;
 
 function Start() {
     firstObject = GameObject.Find("yellowBarrel").GetComponent(RollingLogic);
     firstObject.values = 4;
 }
Comment
Add comment · Show 5 · 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 stick_penguin · Aug 17, 2013 at 09:38 PM 0
Share

Thanks, cleared this topic up for me

avatar image jdellis23 · Apr 09, 2014 at 10:36 PM 0
Share

Getting the following error with this code.

BCE0018: The name 'RollingLogic' does not denote a valid type ('not found').

avatar image Purpose2 · Apr 09, 2014 at 10:57 PM 0
Share

Well, jdellis23 - does the GameObject called "yellowBarrel" in your scene have the script titled "RollingLogic"? That is how this snippet of code works, find object called x, with component y and save it to a variable z. Then you are able to edit a variable on y's script.

avatar image jdellis23 · Apr 10, 2014 at 02:36 PM 0
Share

Yes the GameObject has the script attached to it as a component.

avatar image mavii_ege · May 22, 2014 at 03:31 AM 0
Share

Thanks working !

avatar image
1

Answer by ArkaneX · Aug 17, 2013 at 01:18 PM

If RollingScript is attached to yellowBarrel, than it is one of yellowBarrel's components. You have to access it using GetComponent method.

Comment
Add comment · Show 2 · 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 jdellis23 · Apr 09, 2014 at 10:35 PM 0
Share

Getting the following error with this code.

BCE0018: The name 'RollingLogic' does not denote a valid type ('not found').

avatar image MooOooN · Jan 31, 2015 at 09:57 AM 0
Share

RollingLogic is the name of the script. Write your script name here ins$$anonymous$$d, which you have attached to the gameObject you want to access.

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Changing a variable across multiple instances of a GameObject 1 Answer

How to save a gameObjects on a prefab and variables with a script? 1 Answer

Select colour then change texture 0 Answers

updating a gameobject variable from another script attached to another object 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