• 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
1
Question by kekrot · Feb 27, 2014 at 02:35 AM · gameobjectvariable

How to get variable from another object?

I've searched and searched, but every single answer is something that doesn't work. I get the feeling that the mechanisms they used as answers are no longer available in Unity, like the

 playerScript.insertsomethinghere

That doesn't work. On to my question:

How do I get a variable from another object? I need to find the move speed of the main character from another object, something like this:

         float thisObjectMove = GameObject.Find("mainCharacter").GetComponent("controllerScript".<float move>);

I know that is wrong, so how would I put this?

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
3

Answer by rutter · Feb 27, 2014 at 02:38 AM

Let's break that up into a few steps. It looks like you're using C#.

First, find a GameObject by name:

 GameObject go = GameObject.Find("mainCharacter");

Second, get a component:

 controllerScript cs = go.GetComponent<controllerScript>();

Third, read a public field/property from that component:

 float thisObjectMove = cs.move;

Putting all of that together:

 GameObject go = GameObject.Find("mainCharacter");
 controllerScript cs = go.GetComponent<controllerScript>();
 float thisObjectMove = cs.move;

Splitting that up into multiple lines will make it easier to troubleshoot; compile or null reference errors will reference one line (and thus one step).

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 kekrot · Feb 27, 2014 at 03:11 AM 0
Share

Thanks, but I get this error:

"The type or namespace name `controllerScript' could not be found. Are you missing a using directive or an assembly reference?"

Seems like it can't read "controllerScript" from your second line, so is that really a type of variable?

avatar image rutter · Feb 27, 2014 at 03:12 AM 0
Share

Does your project contain a script class named "controllerScript"? Note that C# is case-sensitive (and that people usually begin class names with a capital letter).

avatar image kekrot · Feb 27, 2014 at 03:16 AM 0
Share

No, where do I set script classes? In the script in where I get the variable, or is it something in the Unity editor? Just searched for it but couldn't find any related info.

avatar image kekrot · Feb 27, 2014 at 04:18 AM 0
Share

I'll add that I'm trying to access a variable that changes all the time, the variable keeps the player's movements plus additional things like running shoes equipped and so on.

Is it possible to fetch the variable's value in the second script's FixedUpdate from the original script's FixedUpdate?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Script that stores gameobject hit by raycast keeps getting a NullReferenceException error. 2 Answers

how can I display a variable as a GUIText 5 Answers

Assigning current color to a variable for fade out (C#) 0 Answers

How to create gameobject with different setting 1 Answer

Communication between objects and other scripts, variables and properties 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