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 /
This question was closed Jan 14, 2014 at 09:39 PM by rednax20 for the following reason:

all's well. my problem is no longer problematic, if you catch my drift

avatar image
0
Question by rednax20 · Jan 09, 2014 at 11:11 PM · gameobjectvariablegetcomponentfindpublic

Find + GetComponent not working as expected

I have a script that should make player two's (named paddle2 in the scene) 'hooped' public variable true.

 function OnTriggerEnter(){
     GameObject.Find("paddle2").GetComponent("randomeffects").hooped = true; // player two's name is paddle2
 }

this works great, until i throw in another player in the scene called 'paddle1', then it sometimes changes paddle1's hooped variable to true instead.

paddle1 and paddle2 have the same scripts on them the only thing different is they're names.

why does this happen? How can i make it not happen?

thank you for your time

have a Quishtay™ day!

Comment
Add comment · Show 13
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 AndyMartin458 · Jan 09, 2014 at 11:28 PM 0
Share

Is it really the same script on both? I wouldn't think so since the other script probably says GameObject.Find("paddle1") right?

avatar image rednax20 · Jan 09, 2014 at 11:32 PM 0
Share

no, sorry, misleading. the script above is on a hoop, not the paddles. the script running the code above is not on either of the paddles and it is not called randomeffects. the hoop script is supposed to access the other scripts

avatar image AndyMartin458 · Jan 09, 2014 at 11:42 PM 1
Share

The GameObject.Find("paddle2") will only ever return the game object with name paddle2 or null. There could be an error in your scene if you forgot to rename paddle1 or ins$$anonymous$$d set the tag ins$$anonymous$$d of the name. $$anonymous$$oreover, if the paddle is not found, you will get a null reference exception when you try to get the component off of a null object.

There isn't a bug in Unity for this, but if you want to be absolutely sure which object is being referenced, try this. $$anonymous$$ake a "public GameObject paddle" variable in your script. Place that script on the objects you like, and, in the inspector, drag the correct paddle from the scene onto the paddle. If you don't want to do that use an Awake function to find the game object, ins$$anonymous$$d of finding it each time the trigger function is called. You can see all of these best practices here http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

The other suggestion I have is to use the angle bracket notation ins$$anonymous$$d of string notation. It is faster than string lookup.

avatar image rednax20 AndyMartin458 · Jan 10, 2014 at 12:05 AM 0
Share

awwww. just figured out what i was doing wrong.

$$anonymous$$y variable in the other script was a static variable. I cant believe I did that.

thank you for you help anyway

avatar image rednax20 AndyMartin458 · Jan 10, 2014 at 12:06 AM 0
Share

although i would really like to know what is should use ins$$anonymous$$d of go.find

avatar image AndyMartin458 AndyMartin458 · Jan 14, 2014 at 06:42 PM 0
Share

@rednax20 well, there is a way to set an object reference in the inspector. That will set the reference before the game ever starts. Go ahead an close the question or post your own answer and select it.

Show more comments
avatar image YoungDeveloper · Jan 09, 2014 at 11:52 PM 0
Share

I think you shouldn't use go.find first of all for such things, in fact, if you can, you shouldn't use it at all.

avatar image AndyMartin458 · Jan 10, 2014 at 12:09 AM 1
Share

You should really be setting the variable in the inspector so that it never has to do a find. If you do use a find, only do it in awake, or somewhere where it only needs to run once.

Show more comments

0 Replies

  • Sort: 

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

Only change a variable on the instaniated object not the prefab. 0 Answers

How to spawn a GameObject by name? 2 Answers

Limiting respawns on scene 1 Answer

Cannot assign Public GameObject variable in Inspector... 2 Answers

How to set a public variable to a gameObject in the inspector through coding 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