The question is answered, right answer was accepted
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 But it just never works, anyone help?
Are you sure the PlayerCameraPosition is instantiated when the Start function is called ?
100% sure, I can see the scene through the camera, but it never finds the gameobject.
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
By "not work", you mean you get a null reference error?
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)
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.
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.
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
Follow this Question
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