Here is my code.
var Sampan : GameObject;
function OnTriggerEnter(coll:Collider)
{
var sampantag = GameObject.FindWithTag("Sampan");
if (coll.gameObject.tag == "Sampan")
{
Instantiate(Cubetest, transform.position, Quaternion.identity);
Destroy(coll.gameObject);
Destroy(gameObject);
}
}
Basically, what I’m trying to do is to destroy this object named “Sampan” and the player and instantiate “Cubetest”.
So far, I’ve managed to destroy myself, instantiate the “cubetest” but am unable to destroy the “Sampan”. Any idea what I’m doing wrong?
Hmm… well, without an idea of where this was supposed to happen in your game, I tried the code as-is in a test area. The trigger object was a sphere with the script assigned to it. The Sampan objects were three capsule objects with the Sampan tag. The cubetest object was a cube. From what I could tell, it operated correctly, deleting the first capsule to collide with the sphere, deleting the sphere and creating the cube in its place. I did notice a couple of things that might not be necessary. I commented out your GameObject declaration variable at the top, as well as the sampantag variable within your function. It still works without those, as your code checks for the appropriate object tag with the if statement.
I suggest that you give it a try with this code and see if you still run into the same error:
var Cubetest : GameObject;
function OnTriggerEnter(coll:Collider)
{
if(coll.gameObject.tag == "Sampan")
{
Instantiate(Cubetest, transform.position, Quaternion.identity);
Destroy(coll.gameObject);
Destroy(gameObject);
}
}
Hi!
A couple of questions in order to try to help…
Why do you have a “var Sampan : GameObject” var?
Is that the object you want to destroy? If it is, try doing a Destroy(Sampan).
Also, why do you do the "var sampantag = GameObject.FindWithTag(“Sampan”); "? I don’t see you using the sampantag var anywhere.
Is the code you posted complete?
var Sampan : GameObject;
var Hut : GameObject;
var pTest : GameObject;
currentPlayer.camera.active = true;
function OnTriggerEnter(coll:Collider)
{
var sampantag = GameObject.FindWithTag("Sampan");
if (coll.gameObject.tag == "Sampan")
{
Instantiate(pTest, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Solved, thanks for your help guys.