I am new to Unity and C# and still learning. I get the following error when i try to instantiate a prefab;
NullReferenceException: Object reference not set to an instance of an object
SpriteScript.OnMouseDrag () (at Assets/Scripts/SpriteScript.cs:49)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32, Int32)
I am using this code where the error occurs:
using UnityEngine;
using System.Collections;
public class SpriteScript : MonoBehaviour {
float waitTime = 1.0f;
float downTime;
bool isHandled = false;
float lastClick = 0;
bool mouseOver = false;
GameObject theMultiIcon;
void OnMouseEnter(){
mouseOver = true;
}
void OnMouseExit()
{
//clear the object under the mouse when dragging rather than click and hold.
mouseOver = false;
isHandled = true;
}
void OnMouseDown () {
print ("OnMouseDown");
//start recording the time when a key is pressed and held.
downTime = Time.time;
isHandled = false;
//look for a double click
if(Time.time-lastClick < 0.3){
// do something
Debug.Log("You double clicked the target.");
}
lastClick = Time.time;
}
void OnMouseDrag() {
//open a menu when the key has been held for long enough.
if((Time.time > downTime + waitTime) !isHandled mouseOver){
isHandled = true;// reset the timer for the next button press
//add markup menue script here.
Debug.Log ("MouseKey was pressed and held for over " + waitTime + " secounds.");
theMultiIcon = GameObject.Find("multi-icon");
theMultiIcon.transform.position = new Vector3(3.2f, 4.5f, 1f);
Instantiate(theMultiIcon);
}
}
}