- Home /
The question is answered in the comments
Click object and it will randomly change its position + add score
Hi we have this code for S_Click.js
private var button: GameObject;
private var ScoresText: GameObject;
function Start(){
ScoresText= GameObject.FindGameObjectWithTag("Player") ;
button= GameObject.FindGameObjectWithTag("Finish") ;
}
function Update()
{
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray =Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit))
{
ScoresText.GetComponent(S_Score).score+=1;
if(hit.transform.tag == "Finish")
{
var position =Vector3(Random.Range(-6,6),Random.Range(-6,6),0);
//Destroy(this.gameObject);
hit.transform.position=position;
}
}
}
}
and then for the S_Score.js
var ScoresText: TextMesh;
var score=0;
function Update(){
ScoresText.text="$ : " + score.ToString();
if (score == 15){
print ("ok");
}
}
what could be so wrong? when I click the object, it should add 1 to the score... it's already changing its position.
thanks iff you help me!
I could not see anything that would cause your code not to add 1, so I tested it in Unity. It worked fine for me including adding 1 to the score and displaying the new score.
really? uhhh... why is it not working on me??? :''(
I have no idea. I started with a new scene with just a 3D text object and a cube. I put the first script on the cube and tagged it "Finish". I put the second script on the 3D text object, dragged the 3D text to the ScoresText variable and tagged the game object 'Player'. You might try this with a new scene to see if it works for you.
okayyy... i tried this and it worked for me! thanks so much robertbu! =))