I’ve just noticed now that my raycast is behaving strangely, when I click on an object I want my ray to get the information of that object and behave accordingly, as of now it seems to multiply that ray each time I click on my objects.
Screenshot when i first click an object:
and heres the second time I click an object:
It has now output as if i’ve hit the second object twice, and this goes up by one each time i’ve clicked on a new object. Does anyone know why this is?
Here is my code:
using UnityEngine;
using System.Collections;
public class selectObject : MonoBehaviour
{
public bool isSelected = false;
public string ballColor = "";
public GameObject selectedBall;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
playNextLevel ();
if (Input.GetMouseButtonDown (0))
{
RaycastHit hitInfo = new RaycastHit ();
bool hit = Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (Input.GetMouseButtonDown (0) && hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.gameObject.tag == "redBall")
{
isSelected = true;
ballColor = "red";
selectedBall = hitInfo.transform.gameObject;
}
else if(hitInfo.transform.gameObject.tag == "blueBall")
{
isSelected = true;
ballColor = "blue";
selectedBall = hitInfo.transform.gameObject;
}
else if(hitInfo.transform.gameObject.tag == "greenBall")
{
isSelected = true;
ballColor = "green";
selectedBall = hitInfo.transform.gameObject;
}
else if(hitInfo.transform.gameObject.tag == "yellowBall")
{
isSelected = true;
ballColor = "yellow";
selectedBall = hitInfo.transform.gameObject;
}
}
}
}
}