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
2
Question by cregox · Feb 24, 2010 at 01:24 AM · variablescriptingbasicsprogramming-basics

Referencing variables / functions on another script

I have 3 objects.

The player contains a child which is just a trigger. I've set GroundCheck to it.

The third is a separate object, a box.

Player and box are rigidbodies, the player child is not.

This is my full GroundCheck.js script:

static var isGrounded : boolean = false;

function OnTriggerStay () {isGrounded = true;} function OnTriggerExit () {isGrounded = false;}

And this is a part of just another script where I wanted to use it:

function FixedUpdate () {
    if (GroundCheck.isGrounded)
}

This was done following instructions on the documentation, and it is working, but not as expected. If I set both scripts just on the player, that's fine. If set both also in the box, when I check for GroundCheck it seems like the "isGrounded" variable is a global one that's just modified by each instance and the final behavior is quite odd from what I hoped.

It's pretty obvious the "another script" isn't specific calling the GroundCheck that I set to a child object or even in the same object - you can see that on the above code. Then how would I do it properly? I thought maybe using static-functions, or maybe using static class, but none of those actually answer my question.

I understand there are several other ways to reference and access other scripts or game objects, but I wonder which one would be best and which one would fit this specific situation.

There are two questions in one here. ;)

edit: I just found out I shouldn't be using "static" there... But I still don't know what should I be using instead.

edit2: here's a continuation to this issue.

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 Motionreactor · Feb 24, 2010 at 10:10 AM 0
Share

Why does the box need both scripts? Is the box a player? I would have thought you would only need to check if the player is grounded, or do you wish to check if the box is grounded, independent of the player?

avatar image cregox · Feb 24, 2010 at 03:28 PM 0
Share

@$$anonymous$$otionreactor Well this is a general program$$anonymous$$g question, I was just giving my instance of usage. So there could be a better way to do what I'm trying, and I'd love to hear it. I wanted to separate this because I used GroundCheck first on the player for other things, and then when I did the second script it would use the GroundCheck, so if I want to use the second I need the first. In the end it's just a way of organizing. In this instance I could just remove the first script and copy the very small code into each place I need it. But what if I want to know if the other object "isGrounded"?

1 Reply

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

Answer by Ashkan_gc · Feb 26, 2010 at 05:34 AM

for referencing other objects there are 3 ways. 1 make that object your child and use GetComponentsInChildren and ... 2 use a public variable and set the reference in inspector. this is a fast method but might mix you up in big projects. 3 use GameObject.Find in Start function to find what you want and store the result in a variable of class scope. each method has it's own usage. the first method is not good for referencing. the second method is good for verry general and reusable Scripts that many people want to use. the third way is the way that you should use for big projects and none reusable codes. when i say reusable i mean something verry general. watch the optimization video of unite 07 for more info.

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 cregox · Feb 26, 2010 at 12:43 PM 0
Share

That's great stuff to know! Now, what if I mix them all? Set a public variable then on Start() if it's null search for GetComponentsInChildren and if it can't find it then use GameObject.Find - How about that?

avatar image Ashkan_gc · Feb 26, 2010 at 01:39 PM 0
Share

it's a good approach because if you or your level designer forget to attach the correct component it will correct it. but it's good to use just one of the second or third methods. if the component is always in your children use the second method but if sometimes it's there and sometimes in other gameObjects then use the third method. it really depends on your project. if %80 of the times it's in your children, what you wrote is the best approach.

avatar image cregox · Feb 26, 2010 at 04:09 PM 0
Share

Just a note: can't use GameObject.Find directly, need to do .GetComponent. Now I'm just trying to make a static function for using on the Start then I'll post it here... As soon as I can get around the issues I'm having with it.

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

why do I need to type variables classes? 2 Answers

Find Object with variable, not a name. 1 Answer

Difference between while loop and dowhileloop? 1 Answer

Instantiate and initialize script 3 Answers

Accessing Variables within another gameobject's 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