- Home /
Detecting when certain objects have been destroyed
Ive tryed multiple variations of gameObject.tag in order to count how many times my bullet comes into contact with another gameObject of tag 'Finish', to i can then say when its hit twice, i can do something else. However its getting getting to 2.
Ive started with a variable 'var tagCount : int = 0;' and tryed all sorts of calculations to add 1 when a collision occurs with the tag, but it gets to 1 and wont add further.
My problem is i have 4 targets in game, but only 2 spawn in each time, and im really having trouble figuring this out.
var noOfTargets : int = 4;
var spawnTargetOne : int;
var spawnTargetTwo : int;
public var myFourTargets : GameObject[];
function Start () {
spawnTargetOne = Random.Range( 0, noOfTargets );
var loopSize : int;
loopSize = noOfTargets - 1;
var howFarToTravel = Random.Range( 1, loopSize );
var loopLand : int;
loopLand = spawnTargetOne + howFarToTravel;
loopLand = loopLand % noOfTargets;
spawnTargetTwo = loopLand;
Debug.Log("the two safe and different numbers are " + spawnTargetOne +" "+ spawnTargetTwo);
myFourTargets[spawnTargetOne].SetActive(true);
myFourTargets[spawnTargetTwo].SetActive(true);
}
How can i check if 'spawnTargetOne' and 'spawnTargetTwo' get destroyed, or come into conact with my bullet or whatever so i can write another function that will spawn one last target on a set location, and when thats destroyed end game?
Ins$$anonymous$$d of destroying them, try doing setActive(false); based on their tag on collision. Destroy() isn't "bad practice" technically, but it's bad practice in my opinion. Takes more to completely destroy an object than to ignore it.
I have a different script doing the destroy than the script that is choosing which targets are active. So if i was to take your advice how would i access the array from the destroy script to say if collision occurs myFourTargets[spawnTargetOne].SetActive(false)?
Answer by ChrisTylr · Oct 03, 2013 at 04:54 PM
Ok, so before reading you answer (which to be honest don't have a clue how id write that in javascript) i managed to find a solution.
My score was working fine so i just built an extra digit around it, when target is destroyed i get 20 points and a certain variable goes up by 1.
in another script i call upon the variable and check to see when the value is 2, when it is it will spawn a target.
and even though i havnt got a target spawning yet, i did get the debug.log line when i wanted it, so its a good sign,
Looks like its sorted, although its probably not the best way, it works
Answer by tw1st3d · Oct 03, 2013 at 04:24 PM
So I was thinking something like this. Now, the code probably wont natively work, but it's a building block for what you might want to use.
using UnityEngine;
using System.Collections;
using System;
public class CheckEnemy : MonoBehavior
{
public YourOtherClass yoc;
public GameObject yourScriptObj;
public void Start()
{
yoc = (YourOtherClass) yourScriptObj.GetComponent(typeof(YourOtherClass));
}
public void OnCollisionEnter(Collison e)
{
foreach(string player in YourOtherClass.playerList)
// Not necessarily a string, could be anything.
{
if(player.tag.Equals((GameObject) e.tag))
{
player.setActive(false);
}
}
}
}
Your answer
Follow this Question
Related Questions
How to keep score? 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
4 blocks destroyed - gravity true 2 Answers
Why does my counter keep going up after pickup is destroyed? 1 Answer
Script for counting number of Turrets and when I destroy all of them I go to the YOU WIN seine 1 Answer