if(col.gameObject.name == "Target"){
Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
Destroy(gameObject.Find("Target"), 0);
at the moment it instantiates target as a prefab however re-labels it Target(Clone)
this means that I Destroy(gameObject.find(“Target”) is irrelevant.
How can I destroy the clone without creating a seperate script?
Mike_L
January 2, 2011, 12:44am
2
well, you could make
if(col.gameObject.name == "Target"){
Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
Destroy(gameObject.Find("Target"), 0);
into
if(col.gameObject.tag == "Target"){
var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
target.tag = "Target";
Destroy(gameObject.FindWithTag("Target"), 0);
thanks JMstudios i am fairly new to this so all your feedback is appreciated.
basically i can see how your script should work but now “Target” none of the attributes in the below script are doing what they are supposed to… any suggestions?
function OnCollisionEnter(col : Collision){
if(col.gameObject.tag == "Target"){
var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
target.tag = "Target";
Destroy(gameObject.FindWithTag("Target"), 0);
rigidbody.AddForce(Vector3(0,power,0));
Debug.Log("Nice!");
score += 1;
}
}
Mike_L
January 2, 2011, 1:10am
4
could you give me all of your script so i can test it?
private var currentPos : Vector3;
var prefab : GameObject;
var power : float = 500.0;
var score : int = 0;
function Update(){
currentPos = transform.position;
}
function OnCollisionEnter(col : Collision){
if(col.gameObject.tag == "Target"){
var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
target.tag = "Target";
Destroy(gameObject.FindWithTag("Target"), 0);
rigidbody.AddForce(Vector3(0,power,0));
Debug.Log("Nice!");
score += 1;
}
}
function OnGUI () {
GUI.Label( Rect(Screen.width - 100, Screen.height - 20, 100, 20), "Counter is: " + (score));
}
What is happening? Is neither object getting destroyed?
Mike_L
January 2, 2011, 1:35am
7
and what exactly is this supposed to do? can you explain exactly?
This ^^
Also, I have something for you
Destroy(gameObject.FindWithTag("Target"), 0);
change this to:
Destroy(GameObject.FindWithTag("Target"));
GameObject should be capitalized, as you are working with the type, and you do not need ‘0’ because that is the default value.
Mike_L
January 2, 2011, 1:50am
9
what is the script supposed to do matty7?
Why? Isn’t it obvious? It creates an object then instantly deletes it!
Mike_L
January 2, 2011, 1:56am
12
im having a hard time figuring this out because i dont know the purpose of the script
Yeah, without context it’s hard to tell the purpose of it.
Is the prefab an explosion or something like that?
What is this script attached to?
Mike_L
January 2, 2011, 2:05am
14
well, this seems to work
private var currentPos : Vector3;
var prefab : GameObject;
var power : float = 500.0;
var score : int = 0;
function Update(){
currentPos = transform.position;
}
function OnCollisionEnter (col : Collision){
if(col.gameObject.tag == "Target"){
var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
target.tag = "Target";
Destroy(GameObject.FindWithTag("Target"));
rigidbody.AddForce(Vector3(0,power,0));
Debug.Log("Nice!");
score += 1;
}
}
function OnGUI () {
GUI.Box( Rect(Screen.width - 100, Screen.height - 20, 100, 20), "Counter is: " + (score));
}
well, this seems to work
private var currentPos : Vector3;
var prefab : GameObject;
var power : float = 500.0;
var score : int = 0;
function Update(){
currentPos = transform.position;
}
function OnCollisionEnter (col : Collision){
if(col.gameObject.tag == "Target"){
var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
target.tag = "Target";
Destroy(GameObject.FindWithTag("Target"));
rigidbody.AddForce(Vector3(0,power,0));
Debug.Log("Nice!");
score += 1;
}
}
function OnGUI () {
GUI.Label( Rect(Screen.width - 100, Screen.height - 20, 100, 20), "Counter is: " + (score));
}
Am I correct in saying that it deletes both the objects?
Mike_L
January 2, 2011, 2:08am
16
what do you mean by both objects?
Both the object that collides, and the object that is made through the Instantiate call.
Mike_L
January 2, 2011, 2:15am
18
this does:
private var currentPos : Vector3;
var prefab : GameObject;
var power : float = 500.0;
var score : int = 0;
function Update(){
currentPos = transform.position;
}
function OnCollisionEnter (col : Collision){
if(col.gameObject.tag == "Target"){
var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
Destroy(target, 5);
Destroy(col.gameObject);
Debug.Log("Nice!");
rigidbody.AddForce(Vector3(0,power,0));
Debug.Log("Nice!");
score += 1;
}
}
function OnGUI () {
GUI.Box( Rect(Screen.width - 100, Screen.height - 20, 100, 20), "Counter is: " + (score));
}
When you have two objects with the same tag, and you call Destroy on the tag, does it only destroy the first it finds, or all of them?
Because:
if(col.gameObject.tag == "Target") // col's tag is "Target"
...
...
var target = Instantiate(...)
target.tag = "Target"; // Target's tag is "Target"
Destroy(GameObject.FindWithTag("Target"));
They both have the same tag, on the call to Destroy, shouldn’t it destroy both?
Mike_L
January 2, 2011, 2:22am
20
actually, it would only destroy one of them. and i updated the script, look above