Question by
Raviraja190 · Jul 25, 2017 at 11:41 AM ·
gameobjectinstancenull reference exceptionnull referencenullreferenceexeption
#Need Fast Help# NullReferenceException: Object reference not set to an instance of an object
using UnityEngine; using System.Collections;
public class DetectTouchesAndClicks : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit))
{
Debug.Log("ObjectName:" + hit.collider.name, hit.collider.gameObject);
hit.collider.SendMessage ("Touched", SendMessageOptions.DontRequireReceiver);
}
}
if (Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
Ray ray = Camera.main.ScreenPointToRay((Vector3)touch.position);
RaycastHit hit;
if(Physics.Raycast(ray,out hit))
{
Debug.Log("ObjectName:" + hit.collider.name, hit.collider.gameObject);
hit.collider.SendMessage ("Touched", SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Not sure what this error is telling/asking me to do? 1 Answer
How to affect only one object out of many objects with the same name using script? 0 Answers
Object reference; sometimes works but sometimes doesn't??? 2 Answers
[SOLVED] NullReferenceException in a script that calls another object's script variable 1 Answer
c# Object reference not set to an instance of an object 1 Answer