- Home /
Touch destorying too many objects
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.
Answer by Fr0sZ · Jun 30, 2011 at 06:55 PM
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.
yeah its on the prefab which gets instantiated.....thanks a lot for the answer...i thought it would be something simple
Hi guys, above script is attached to camera or object which is to be destroyed ?
@gokulreddy it does not matter. as long as you're using Camera.main.ScreenPointToRay it will work from any object and have the same effect.
Your answer
Follow this Question
Related Questions
Destroying GameController Respawns? 0 Answers
How to Destroy multiple object on one hit? 1 Answer
input touch android help 0 Answers
Touch to destroy specific object 2 Answers
How to destroy object by touch? 2 Answers