Search Unity

Unity Coroutines Tutorial giving an error

Discussion in 'Scripting' started by Ciix, Mar 7, 2015.

  1. Ciix

    Ciix

    Joined:
    Jun 19, 2013
    Posts:
    9
    I'm tying to use the Unity tutorial on Coroutines located here:
    http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

    I'm on the second part of the video where he uses his mouse click to make the robot move to that position. But when I use the provided scripts, I am getting the error:

    NullReferenceException: Object reference not set to an instance of an object
    ClickSetPosition.OnMouseDown () (at Assets/_Scripts/ClickSetPosition.cs:19)
    UnityEngine.SendMouseEvents: DoSendMouseEvents(Int32, Int32)

    I have a plane with a cube on it. The plane has the ClickSetPosition.cs script attached and the cube has the PropertiesAndCoroutines.cs script attached.

    Here are the scripts (these are the one's from Unity's page at the link above):

    Any help would be great :)

    ClickSetPosition.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClickSetPosition : MonoBehaviour
    5. {
    6.     public PropertiesAndCoroutines coroutineScript;
    7.    
    8.    
    9.     void OnMouseDown ()
    10.     {
    11.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.         RaycastHit hit;
    13.        
    14.         Physics.Raycast(ray, out hit);
    15.        
    16.         if(hit.collider.gameObject == gameObject)
    17.         {
    18.             Vector3 newTarget = hit.point + new Vector3(0, 0.5f, 0);
    19.             coroutineScript.Target = newTarget;
    20.  
    21.         }
    22.     }
    23. }
    PropertiesAndCoroutines.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PropertiesAndCoroutines : MonoBehaviour
    5. {
    6.     public float smoothing = 7f;
    7.     public Vector3 Target
    8.     {
    9.         get{ return target; }set
    10.         {
    11.             target = value;
    12.            
    13.             StopCoroutine("Movement");
    14.             StartCoroutine("Movement", target);
    15.         }
    16.     }
    17.    
    18.    
    19.     private Vector3 target;
    20.    
    21.    
    22.     IEnumerator Movement (Vector3 target)
    23.     {
    24.         while(Vector3.Distance(transform.position, target) > 0.05f)
    25.         {
    26.             transform.position = Vector3.Lerp(transform.position, target, smoothing * Time.deltaTime);
    27.            
    28.             yield return null;
    29.         }
    30.     }
    31. }
     
  2. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    In ClickSetPosition.cs, have you assigned the coroutineScript variable to a PropertiesAndCoroutines script?
     
  3. Ciix

    Ciix

    Joined:
    Jun 19, 2013
    Posts:
    9
    Ack ... I forgot to assign the box :(

    Thanks :)