- Home /
Spawned prefabs not keeping their Look At funtion
Hi, I have a prefab with a basic "Look At" script attached (it´s orientation is directed towards the camera). When the prefab is instantiated I would like to keep that orientation. I guess I should either use some Quaternion rotation instead of the Transform or try to include the "look at" function in the "Spawn code"? I am new to scripting and grateful for any hints on this.
void SpawnVistors () { int spawnIndex = Random.Range (0, SpawnPoints.Length); Instantiate (Visitors, SpawnPoints[spawnIndex].position, SpawnPoints[spawnIndex].Rotation); }
Provide details of your LookAt script. Do you want to keep the instantiated prefab to keep looking towards the camera?
Answer by margxsi · Oct 11, 2017 at 10:54 AM
Yes, I want the instantiated prefabs to keep looking towards the camera (tagged as "player").
here is the LookAt script:
using UnityEngine; using System.Collections;
public class LookAtPlayer : MonoBehaviour
{ public Transform player; void Update() { if(player !=null) { transform.LookAt (player); } } }
Answer by ASPePeX · Oct 11, 2017 at 12:01 PM
As far as the code you posted goes, the player variable will always be null since prefabs cannot hold a reference to the scene. You need to get the reference to the player at runtime. The quickest way to get a result is set the tag of you player to "Player" and then get the player reference with "GameObject.FindGameObjectWithTag("Player").transform;".
Your answer
Follow this Question
Related Questions
How to set up random spawnpoints? 1 Answer
how can i spawn my 52 cards in list of my 52 spawn points gameobject? 1 Answer
How do I call SpawnPoints,SpawnLocation,Gameobject,and spawn at certain time 1 Answer
My Player's Spawnpoint is to far away 0 Answers
Graphic assets sometimes look wrong 1 Answer