- Home /
[Solved]cant disable/enable scripts
private CameraFollow follow;
private CameraFollow follow;
private MouseLook look;
// Use this for initialization
void Start () {
transform.parent = null;
look = Camera.main.GetComponent<MouseLook>();
follow = Camera.main.GetComponent<CameraFollow>();
}
// Update is called once per frame
void Update ()
{
GameObject target = GameObject.Find("pc");
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.parent = target.transform;
look.enabled = false;
follow.enabled = true;
}
if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.parent = null;
look.enabled = true;
follow.enabled = false;
}
}
I try to disable my third person camera on zoom in and disable my first person camera on zoom out but this script tends to error nullreference object I fail to see what is my error
@xCeas your question title says it's solved. Did the answer below help you to? if so, you should tick it as correct ;)
It didn't help but for closing I'll mark it so, I just put the change camera person in the CameraFollow script and solved it.
Answer by KellyThomas · Dec 23, 2013 at 03:30 PM
The first thing I notice is that you have the top two lines repeating themselves, I doubt this would compile so it must be a copy/paste error?
The next thing I notice is that these three lines could result in null references:
look = Camera.main.GetComponent<MouseLook>();
follow = Camera.main.GetComponent<CameraFollow>();
GameObject target = GameObject.Find("pc");
It is good practice to perform null checking (at least when debugging) after operations like these.
It can be as simple as:
look = Camera.main.GetComponent<MouseLook>();
if(look == null) {
Debug.Log("GetComponent<MouseLook>() returned null");
}
If you perform a similar check after each line identified above you should be able to determine when the null
s are creeping in.
it retuns null like you suggested but that's wierd since I added the main camera manually both of the scripts
Your answer
![](https://koobas.hobune.stream/wayback/20220613124733im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Fade an object in and out 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Enable/Disable AI script 1 Answer
Help With Disabling/Enabling single GameObject 1 Answer
Disable & Re-enable Script 1 Answer