So im making a game and i got stuck,
ive allowed my player to spawn a block, using instantiate and the block is named as follows - Cube1,Cube2,Cube3 ect
then watching this tutorial - Unity 3D First Person Object Interaction Scripting Tutorial Part 1 - YouTube
i see how he makes his cube change color, but he adds a script to an already created object whereas mine is instantiated, im not sure how i can do this but here was my attempt -
void Update () {
Ray ray = Player.GetComponentInChildren<Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast(ray, out hit, 10))
{
hit.collider.gameObject.GetComponent<Interact>().OnLookEnter();
GameObject isLookingAt = hit.transform.gameObject;
hit.collider.gameObject.GetComponent<Renderer>().material.color = Color.red;
}
}
and here is the error i got - NullReferenceException: Object reference not set to an instance of an object
Select.Update () (at Assets/Code/Select.cs:22)
any ideas on how i can solve this issue and have my mouse change the object color when i look over it.
using UnityEngine;
using System.Collections;
public class Interact : MonoBehaviour {//Mockup-class!
public void OnLookEnter()
{
Debug.Log("Start looking at "+this.name);
}
public void OnLookLeave()
{
Debug.Log("Not looking at "+this.name+" anymore");
}
}
Select.cs
using UnityEngine;
using System.Collections;
public class Select : MonoBehaviour
{
public GameObject ObjectToInstantiate;
private GameObject lastGameObject;
// Use this for initialization
void Start()
{
this.lastGameObject = null;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W)) //Test-spawning
{
float x = Random.RandomRange(0.0f, 5.0f);
float y = Random.RandomRange(0.0f, 5.0f);
float z = Random.RandomRange(0.0f, 5.0f);
GameObject nObj = Instantiate(ObjectToInstantiate, new Vector3(x, y, z), Quaternion.identity) as GameObject;
nObj.AddComponent<Interact>();
}
Camera cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponentInChildren<Camera>();
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
GameObject hitGameObject = null;
if (Physics.Raycast(ray, out hit))
{
hitGameObject = hit.transform.gameObject;
}
if (hitGameObject!= this.lastGameObject)
{
if (this.lastGameObject!= null)
{
Interact lastInteract = this.lastGameObject.GetComponent<Interact>();
if(lastInteract!=null) {
lastInteract.OnLookLeave();
}
Renderer rendLeave = this.lastGameObject.GetComponent<Renderer>();
if (rendLeave != null)
{
rendLeave.material.color = Color.white;
}
}
if(hitGameObject!=null) {
Interact curInteract = hitGameObject.GetComponent<Interact>();
if(curInteract!=null) {
curInteract.OnLookEnter();
}
Renderer curRend = hitGameObject.GetComponent<Renderer>();
if (curRend != null)
{
curRend .material.color = Color.red;
}
}
this.lastGameObject= hitGameObject;
}
}
}
You have to replace the test-parts (spawning, getting the camera, constructing the ray) with your methods. Note that OnLookEnter is only called once the pointer (or in your case the middle of the screen) enters the object (I figured from the method-name, that that is what you want), the same applies to OnLookLeave.