mabit
1
In my game I have random spawning object that the player click/touches to pick up to give energy.
This works fine on PC/WEB with the following code.
function OnMouseDown(){
if (Level.sunOrbPower < 100){
Level.sunOrbPower = Level.sunOrbPower+5;
}
Destroy(gameObject);
}
On Android if the player touches one of the objects they are all destroyed. Only the one being touched should be destroyed. The code I am using is:
function Update(){
touchToPickUp();
}
function touchToPickUp(){
if(Input.touchCount == 1) {
var touch : Touch = Input.touches[0];
if (touch.phase == TouchPhase.Ended) {
ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
if(Physics.Raycast(ray.origin, ray.direction * 100,hit)){
Debug.Log(hit.transform.name);//Object you touched
if (hit.transform.name == "SunOrb"){
if (Level.sunOrbPower < 100){
Level.sunOrbPower = Level.sunOrbPower+5;
}
Destroy(gameObject);
}
}
}
}
}
Any advise would be much appreciated.
Fr0sZ
2
replace
Destroy(gameObject);// this destroy every object that has the script
with
Destroy(hit.gameObject);// just destory the object you hit.
Also i guess that you have the script on every object. You can insted just have the script on the camera.