Problem with Js script : Object reference not set to an instance of an object

This is the script :

function Update () {
    
    if(Input.GetMouseButtonDown(0) ) {
       
        var hit : RaycastHit;
        var anima = new Ray (transform.position , Vector3.forward ) ;
       
        var nomecamu : String = "Cam" + ToString() ;
        var nomecamd : String = "Cam" + hit.collider.ToString();
       
        GameObject.Find(nomecamd).active = true;
        GameObject.Find(nomecamu).active= true;
       
        GameObject.Find(hit.collider.ToString()).GetComponent(MouseLook).enabled = true ;
        GameObject.Find(hit.collider.ToString()).GetComponent(FPSInputController).enabled = true ;
        
         GetComponent(FPSInputController).enabled = false;
         GetComponent(MouseLook).enabled = false ;
     }



}

All time that I press left click the game stops and Console tell me :“Object reference not set to an instance of an object” .

Colliders ’ names is the same of the game object and Cameras’ name are Cam plus the nam of the object.

I hope that someone can help me.

Thank’s first!!

Well, you have multiple GameObject.Find and GetComponent calls that aren’t checked for a valid result. Any one of those things could be failing. You need to check that the results of each of those calls isn’t null prior to trying to access any reference they may contain.

Jeff

So something like this

function Update () {
    
    if(Input.GetMouseButtonDown(0) ) {
       
        var hit : RaycastHit;
        var anima = new Ray (transform.position , Vector3.forward ) ;
       
        var nomecamu : String = "Cam" + ToString() ;
        var nomecamd : String = "Cam" + hit.collider.ToString();
       
        if ( GameObject.Find(nomecamd) != null && GameObject.Find(nomecamu) != null ){
       
            GameObject.Find(nomecamd).active = true;
            GameObject.Find(nomecamu).active= false;
        }
       
        if(GameObject.Find(hit.collider.ToString()) != null){
            GameObject.Find(hit.collider.ToString()).GetComponent(MouseLook).enabled = true ;
            GameObject.Find(hit.collider.ToString()).GetComponent(FPSInputController).enabled = true ;
         }
        
         GetComponent(FPSInputController).enabled = false;
         GetComponent(MouseLook).enabled = false ;
     }



}

But don’t work :frowning:

The error should tell you the line the null reference is happening on.

I’m guessing line 9, where you use hit.collider.ToString() - not sure what you’re expecting there but you haven’t passed that RaycastHit object to any actual raycasts (creating a RaycastHit or Ray object does not perform a raycast, they’re simply data structures). Without a performing a Physics.Raycast to populate the RaycastHit data structure of course it’s going to be null.

You also really shouldn’t be using GameObject.Find in the update loop, that’s going to be horrifically slow – you could simply use hit.collider.gameObject as every Component class has a reference to the GameObject it’s attached to.

Thank’s…I want to create a script that change character with raycast…Some idea abut that?

Don’t want to sound cruel here but you’ll honestly be best served by spending more time following programming tutorials (generic ones, preferably C#, not Unity tutorials) before jumping into writing gameplay scripts. There’s so many things you don’t seem to be aware of and you’ll likely hit several walls every time you take another step. Perhaps look in the education forums for some good resources for learning.