- Home /
How ?Attach camera to parent
Hello, sorry for my bad english.
When in enttering in trigger my camera is detached of parent but i want if player leave trigger camera is re child of Player.
I have this error :
NullReferenceException: Object reference not set to an instance of an object CameraBattle.OnTriggerExit (UnityEngine.Collider other) (at Assets/script/CameraBattle.cs:37)
The script is attached to Camera
Camera cam;
GameObject player;
// Use this for initialization
void Start()
{
cam = GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
}
// When player enter trigger detach camera
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Ennemis")
{
cam.transform.SetParent(null);
}
}
// When player leave trigger attach camera
public void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Ennemis")
{
cam.transform.parent = player.transform;
}
}
}
Answer by Martin_Gonzalez · Mar 30, 2018 at 09:42 PM
Just change the OnTriggerExit method with this
public void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Ennemis")
{
cam.transform.SetParent(player.transform);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612155717im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The objects use the scripts in the assets but they don't appear there when I click on them. 0 Answers
using unity string plugin webcam 1 Answer
Get UnityEngine.Object reference to folder in project from folder path 2 Answers
Shooting an object through a tube of cannon in a projectile manner. 0 Answers
How do I Adjust The Colour Of My Object By Percentages? 1 Answer