- Home /
onclick on random objects in gamemode and update score
Here I want to Choose a correct DYNAMIC RANDOM Object answer,compare two objects in monitor mouse position and select the correct object means update a score.am stuck with if ((hit.collider.gameObject).Equals(dub)) help me,
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic;
public class Rannum : MonoBehaviour { private TextMesh rnum; public TextMesh ans; public TextMesh anss; private TextMesh ansd1; private TextMesh ansd2; public GameObject[] aa; public GameObject dub; public RaycastHit hit;
void Start ()
{
int n;
n = Random.Range (1, 100);
print (n);
GameObject texq = GameObject.Find ("TextQ");
rnum = texq.GetComponent<TextMesh> ();
rnum.text = "Number:" + n;
int x = n;
int y = (x / 3);
int z = x - y;
print (y);
print (z);
int n1 = (n - 1);
int d1 = (n1 / 3);
int d2 = n1 - d1;
int d3 = Random.Range (0, 50);
int index = Random.Range (0, aa.Length);
print ("index:"+index);
//int a = 0;int b = 1;int c = 2;
GameObject t1 = GameObject.Find ("Text1");
GameObject t2 = GameObject.Find ("Text2");
GameObject t3 = GameObject.Find ("Text3");
if (index < 1) {
ansd1 = t2.GetComponent<TextMesh> ();
ansd1.text = +y + "+" + d3;
ansd2 = t3 .GetComponent<TextMesh> ();
ansd2.text = +d1 + "+" + d2;
}
if (index > 0 || index < 2) {
ansd1 = t1.GetComponent<TextMesh> ();
ansd1.text = +y + "+" + d3;
ansd2 = t3.GetComponent<TextMesh> ();
ansd2.text = +d1 + "+" + d2;
}
if (index > 1) {
ansd1 = t1.GetComponent<TextMesh> ();
ansd1.text = +y + "+" + d3;
ansd2 = t2.GetComponent<TextMesh> ();
ansd2.text = +d1 + "+" + d2;
}
dub = aa [index];
ans = dub.GetComponent<TextMesh> ();
ans.text = +z + "+" + y;
}
void Update()
{
if (Input.GetMouseButtonDown (1)) {
Application.LoadLevel (0);
}
Ray ray = camera.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown (0))
{
if (Physics.Raycast (ray, out hit, 10))
{
if ((hit.collider.gameObject).Equals(dub))
{ print ("write the code for score...!!!");}
else { print("loose"); }
}
}
}
}
Answer by hbalint1 · Apr 15, 2015 at 03:44 PM
if ((hit.collider.gameObject).Equals(dub))
You should never use this, unless you have overriden the Equals() method!
just use this instead:
if (hit.collider.gameObject == dub)
this compares them by reference.
If you want some more complex comparing method, you must override Equals() and use it.