How to get the camera to follow a prefab?
Hiya Unity CommUnity!
I'm writing a script to spawn my player prefab whenever I load a new scene. (I may or may not be doing spawning entirely wrong. Would appreciate input if you have any.)
In any case, I'm trying to get the Main Camera to search for a Player prefab through code and follow that, but what I'm writing is not working and I don't quite have enough experience yet to figure out why. Here is the relevant code:
var player : Transform;
function Update(){
if (player == null){
var player : GameObject = GameObject.FindGameObjectsWithTag("Player")[0];
}
Debug.Log(player);
}
The Debug.Log prints out 'null' once (which is odd; I would expect it to print out over and over again, being in the Update function), so I'm sure that everything is being called correctly. Do you know what's wrong with this code?
Any feedback is appreciated. Thanks a lot!
Answer by idontwannabeauser · Oct 26, 2016 at 06:41 AM
@rainbowparadoxes: You might like check this out: https://unity3d.com/es/learn/tutorials/projects/2d-ufo-tutorial/following-player-camera One important thing they said here is that you should use LateUpdate()
instead of Update()
for camera following behavior since it is called after all other objects are updated so you ensure the right update for the camera.
Answer by Brocccoli · Mar 23, 2016 at 06:47 PM
You are only seeing Debug.Log once because Unity nests the same Logs by default. You can expand the single log line to see every line.
For the problem you're having however, I believe there may be a better way to go about such a problem. Below is the code I am currently using on my camera object in my current prototype.
public Transform target;
public int distance = -10;
void Update ()
{
this.transform.position = new Vector3 (target.transform.position.x, target.transform.position.y, distance);
}
In the editor I can set my PlayerPrefab's transform as the target and have it follow that. This reduces some load at Startup. (Be aware, the above is C# and will differ if your implementing in JS or Boo)
If you're set on your previous way, than I would suggest double checking that your Player prefab actually has the tag of "Player". Make sure it's exact as I'm pretty sure the method is case sensitive.
Also, it may be a little cleaner (if you're only looking for one player) to use
GameObject.FindWithTag("Player");
which will return a single GameObject and remove the unnecessary indexing.
I was only showing you the part of the code where I defined the 'player' object. $$anonymous$$y camera follow code looks fine, and is very similar to yours. (It's in the LateUpdate function, though.) I have dragged my prefab into the editor, but it didn't work (it worked well with normal gameobjects). I want this code to work in all of the levels, not just one, so I can't just drag one instance of a prefab into the editor- which is why I wrote my code.
Thanks for showing me the FindWithTag function! I'd been using a workaround because I didn't know a singular version existed. XD
The prefab's tag is definitely 'Player', capitalized (i'm using the default tag).
Thanks for all your help! $$anonymous$$y code is a little prettier now. If I find out my issue, I'll post it to the board.