- Home /
 
What is wrong with my zipline script?
Hi, I am making a zipline thing, and I'm almost done, but there's one error that I get when I walk over a trigger, it says "NullReferenceException: Object reference not set to an instance of an object Zipline.OnTriggerEnter (UnityEngine.Collider Other) (at Assets/Zipline.js:15)" What is wrong here?
Here's my script:
 var EndPoint : Transform;
 
 public var IsRiding = false;
 
 var PlayerPhysic : CharacterMotor;
 
 var Player : GameObject;
 
 var Speed : float;
 
 function OnTriggerEnter(Other : Collider){
     
     if(Other.gameObject.tag == "Player"){
     
     Player.position = Vector3.MoveTowards(Player.position, EndPoint.position, Speed * Time.detlaTime);    
         IsRiding = true;
         
         }
     
    }
    
    
    function Update () {
        
        if(IsRiding == true){
            
        PlayerPhysic.enabled = false;    
            
            
        }
        
   
        
             if(IsRiding == false){
            
        PlayerPhysic.enabled = true;    
            
            
        }
        
        
        
    }
 
              Answer by ArkaneX · Nov 19, 2013 at 04:34 PM
It looks like your Player or EndPoint variable is not assigned, so it is null. Trying to access any member of such null variable results in NullReferenceException... 
No, you see, I have assigned them properly, I think there is something else that is wrong. It mentions line #15 when I get the error, so I'm guessing I have done something wrong with my script.
Please add this just before line 15, i.e. Player.position = Vector3.$$anonymous$$oveTowards(...)
 print(Player);
 print(EndPoint);
 
                  and see what is printed to the console.
It prints
Player (UnityEngine.Transform) UnityEngine.$$anonymous$$onoBehaviour:print(Object)
EndPoint (UnityEngine.Transform) UnityEngine.$$anonymous$$onoBehaviour:print(Object)
But I've managed to make it move, but the thing is that it moves in just one millisecond, it doesn't have a smooth effect between the positions:
    function OnTriggerEnter(Other : Collider){
     
     if(Other.gameObject.tag == "Player"){
     
          print(Player);
        print(EndPoint);
     
     Player.transform.position = Vector3.Lerp(Player.transform.position, EndPoint.transform.position, 3 * Time.deltaTime);
         IsRiding = true;
         
         }
     
    }
    
                 If it prints correct values, then you probably made some fix ;)
Regarding movement - you can use lerping in coroutine to achieve smooth effect. For example please see this answer.
Answer by ValrikRobot · Mar 12, 2015 at 01:09 PM
You could always try this, very easy to use http://u3d.as/content/valrik/zip-n-swing
Your answer