- Home /
how to re reference a target if a game object is destroyed?
I was previously having an issue with don't destroy on load. I was getting multiple copies every time I went into the orignal scene. I found a fix with this....
using UnityEngine; using System.Collections;
public class DontDestroy : MonoBehaviour { public static DontDestroy control;
void Awake()
{
if (control == null)
{
DontDestroyOnLoad(transform.gameObject);
control = this;
}
else if (control != this)
{
Destroy(transform.gameObject);
}
}
} basically if control does not exist on scene load it wont destroy, but if it does exist it will destroy the other copy.........this is what I wanted,but now I'm running into another problem with the camera follow.......the camera is set to follow the target game controller, but when it gets destroyed it looses it's target in the inspector tab, and it stops following the player because it has no target.....here is the camera follow code..............
using UnityEngine; using System.Collections;
public class CameraFollow : MonoBehaviour {
public Transform target;
Camera mycam;
public float m_speed = 0.1f;
// Use this for initialization
void Start () {
mycam = GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
mycam.orthographicSize = (Screen.height / 100f) / 4f;
if (target)
{
//from.....to......
transform.position = Vector3.Lerp(transform.position, target.position, 0.1f) + new Vector3(0,0,-10);
}
}
}
I'm both new to unity and c#, My question is how would I make a new reference or set the loaded object as the new target so the camera will keep following the player when the old object gets destroyed?
Your answer
Follow this Question
Related Questions
Will Unity support morph targets built in natively? 3 Answers
Semi-Controlled Array Randomization 1 Answer
Tell if navmeshagent has reached target? 0 Answers
Making a target move when hit 3 Answers
Get list of nearby targets (JS) 1 Answer