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 anon · Apr 11, 2010 at 09:52 PM · var

accessing a var in the script of one object from the script of another object

I have a Game object "AI bath tub" which has a script "AICar_Script" attached to it. I want to access several int variables that are in this script from the camera smooth follow script.

in the smooth follow script I have created a GameObject var and dragged AI bath tub to it. Now I was trying to use GetComponent to access the script and the variables in it but this doesn not work. how can I do this?

Comment
Add comment · Show 1
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 Eric5h5 · Apr 11, 2010 at 10:26 PM 0
Share

GetComponent does work. You should post your code; otherwise it's impossible to say what's wrong.

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Peter G · Apr 12, 2010 at 12:35 AM

Normally I would use GetComponent(ScriptName).variable instead of static variables because static creates one instance of a variable which is sometimes helpful, but let's say you have a collection of enemies. If their health is static and one takes damage, they all take damage because they share the same variable which is usually undesirable.

Make sure you have your AICar_Script compiled before your SmoothFollow script. See this page to see the compilation order. The scripts are as simple as:

function Update () {
    var smoothSpeed = GetComponent(AICar_Script).speed;
}
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 chearner · Apr 29, 2010 at 03:35 AM 0
Share

I am using GetComponent to get one JS scripts var in another JS script per above. I get 'myvar' is not a member of 'UnityEngine.Component' error. Any reason why, the var is there and same folder, both JS scripts.

avatar image chearner · Apr 29, 2010 at 04:21 AM 0
Share

i figured this out but, problem being you have to find the object first and then access the script using getcomponent and when using the iphone you have to type it. Not very elegant if you ask me, I should be able to just find a script and extract a var, but i guess the var could have a different value based on what object it is attached to.

avatar image
0

Answer by MooseTrap · Apr 11, 2010 at 10:36 PM

I'm only just learning how to do this myself at the moment, but I believe that one thing to check is make sure the variables you want to access are "static var" not just "var", if you want to access them from another script. This makes them public/global so other scripts can access them.

(Correct me if I'm wrong, somebody. But I was having trouble with this, and doing this made it work for me.)

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 dhendrix · Apr 11, 2010 at 10:48 PM 0
Share

You can use GetComponent to get a script reference then access variables from that, whether the script is attached to the same GameObject or a different one. You don't have to declare the variable as static. However by declaring them as static, you will be able to access them without a script reference.

avatar image MooseTrap · Apr 11, 2010 at 10:51 PM 0
Share

Ooh! Cheers, I didn't realise that!

avatar image Eric5h5 · Apr 12, 2010 at 12:27 AM 0
Share

Declaring variables as static makes them unique. You should normally use GetComponent unless you have a specific reason to use static.

avatar image
0

Answer by Ashkan_gc · Apr 26, 2010 at 04:13 AM

in your smooth follow add this on top

var o:AIbathbup;

then drag that GameObject that has the script AI bath bup on this variable of smooth follow and simply use it's variables like this

o.x=10;

you need to attach AI bath bup to a gameObject. if you want to use the GameObject itself then use

var o:GameObject;

then use

o.GetComponent(AIbathbup).x=10;

also you can store the result of getComponent in a variable and always use that variable. let's say you have a variable called s then you can write

function Awake ()
{
s=o.GetComponent(AIbathbup);
}

then in all parts of your code just use s.varname = ...

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Script to recive varible from toher script and do something with it 1 Answer

Changing a script value 1 Answer

On var enter 1 Answer

Can't get var from other script. 1 Answer

Script that stops other script from working 0 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