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 /
  • Help Room /
This question was closed Mar 07, 2016 at 06:10 PM by Namaarie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Namaarie · Mar 07, 2016 at 07:22 AM · unity5gameobject.find

Why doesnt Gameobject.find not work?

I'm using Gameobject.find, but it won't return anything

     public float smooth = 1F;
     public GameObject thePlayer;
 
     void Start(){
         thePlayer = GameObject.Find("PlayerCameraPosition");
     }
 
     void Update () {
         if(transform.position.x != thePlayer.transform.position.x){
             transform.position = Vector3.Lerp(transform.position, thePlayer.transform.position, Time.deltaTime * smooth);
         }
 
         if(transform.position.y != thePlayer.transform.position.y){
             transform.position = Vector3.Lerp(transform.position, thePlayer.transform.position, Time.deltaTime * smooth);
         }
     }

That's my script on the camera, its suppose to find the position parented to the player, so the camera can lerp to it... in theory, but it never finds anything. As you can see, it is in the hierarchy alt text But it just never works, anyone help?

capture.png (2.0 kB)
capture.png (2.0 kB)
Comment
Add comment · Show 5
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 Hellium · Mar 06, 2016 at 02:34 PM 0
Share

Are you sure the PlayerCameraPosition is instantiated when the Start function is called ?

avatar image Namaarie Hellium · Mar 07, 2016 at 02:01 AM 0
Share

100% sure, I can see the scene through the camera, but it never finds the gameobject.

avatar image Hellium Namaarie · Mar 07, 2016 at 11:31 AM 0
Share

Event if you can see the scene through the camera, when Start is called, the PlayerCameraPosition may not be instantiated. You can check this smy adding a Debug.Log in the Start function of your script here, and in a PlayerCameraPosition Script

avatar image tanoshimi · Mar 07, 2016 at 07:26 AM 0
Share

By "not work", you mean you get a null reference error?

avatar image JoshuaMcKenzie · Mar 07, 2016 at 07:51 AM 0
Share

I'm seeing the word "network". Exactly how are you spawning the player position, and what script is this on? are you using UnityEngine.Networking, with NetworkBehaviour?

also theres the possiblity that you did find it but the gameobject was later deleted (your Update doesn't check for null).

Also thats not how you should be using Lerp...that will give a "rubberbanding" effect (as in moves faster the farther away it is), but will theoretically never reach the target (though it will eventually due to floating-point accuracy). its likely that you want to use $$anonymous$$ovetowards. if you want that Rubberband effect, lerp does wonders, but usually you'll still need $$anonymous$$oveTowards so that theres a $$anonymous$$imum speed too. (check up on Unity's Live Session Tips and Tricks 4 for a more in-depth on lerp)

3 Replies

  • Sort: 
avatar image
3
Best Answer

Answer by Fredex8 · Mar 07, 2016 at 09:49 AM

PlayerCameraPosition is a child of NetworkPlayer and therefore a transform and not a GameObject.

Instead you should probably do something like this

 thePlayer = GameObject.Find("NetworkPlayer");
 thePlayerPosition = thePlayer.transform.GetChild(0);


I believe that Transform.Find will also work but I think that would probably be less efficient (and GameObject.Find is hardly efficient at the best of times). Using GameObject.FindWithTag will be a bit more efficient but if 'NetworkPlayer' means that you are trying to create a multiplayer environment you will need a better solution in the end.

Comment
Add comment · Show 1 · 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 Namaarie · Mar 07, 2016 at 06:10 PM 0
Share

This actually worked, thanks!

avatar image
1

Answer by EmHuynh · Mar 07, 2016 at 08:36 AM

Hello, @Namaarie.

There are many reasons to why GameObject.Find would fail. A common reason is calling it before the instantiation of the object that it seek.

This is probably what is happening:

  • ( ... )

  • Call GameObject.Find

  • ( ... )

  • Call Object.Instantiate

In short, we cannot find something that has not been instantiated.

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
avatar image
0

Answer by SaurabhStudio · Mar 07, 2016 at 09:43 AM

Hello, @Namaarie GameObject may not have been created before you call GameObject.Find Make sure your GameObject is Instantiate before GameObject.Find is called

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

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Unity not opening post Windows update 0 Answers

Generate wind while moving 1 Answer

EasyAR object jitters 2 Answers

(DISSOLVED) Error : Failed to build apk. (Unity 2017.2.0f3) 2 Answers

Cannot modify com.unity3d.UnityEditor5.x.plist via command line 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