I have this C# segment of code to make auto generated rooms, does anyone know why my code won’t delete the clones of the object? I’ve tried a lot of methods, but I can’t for the life of me get it to work. The project is in 3D.
{
[SerializeField] private int intRandomValue;
public float targetTime = 5.0f;
public Vector3 SpawnPos;
//public testscore script;
void Start()
{
SpawnPos = new Vector3(0, 0, 0);
}
void FixedUpdate()
{
intRandomValue = Random.Range(0, 6);
if (intRandomValue == 1)
Instantiate(Cube, SpawnPos, Quaternion.identity);
if (intRandomValue == 1)
tag = "NewRoom";
if (intRandomValue == 1)
SpawnPos += new Vector3(0, 5, 0);
if (intRandomValue == 1)
tag = "Room";
if (intRandomValue == 2)
Instantiate(Cube, SpawnPos, Quaternion.identity);
if (intRandomValue == 2)
tag = "NewRoom";
if (intRandomValue == 2)
SpawnPos += new Vector3(0, -5, 0);
if (intRandomValue == 2)
tag = "Room";
if (intRandomValue == 3)
Instantiate(Cube, SpawnPos, Quaternion.identity);
if (intRandomValue == 3)
tag = "NewRoom";
if (intRandomValue == 3)
SpawnPos += new Vector3(5, 0, 0);
if (intRandomValue == 3)
tag = "Room";
if (intRandomValue == 4)
Instantiate(Cube, SpawnPos, Quaternion.identity);
if (intRandomValue == 4)
tag = "NewRoom";
if (intRandomValue == 4)
SpawnPos += new Vector3(-5, 0, 0);
if (intRandomValue == 4)
tag = "Room";
if (intRandomValue == 5)
Instantiate(Cube, SpawnPos, Quaternion.identity);
if (intRandomValue == 5)
tag = "NewRoom";
if (intRandomValue == 5)
SpawnPos += new Vector3(0, 0, 5);
if (intRandomValue == 5)
tag = "Room";
if (intRandomValue == 6)
Instantiate(Cube, SpawnPos, Quaternion.identity);
if (intRandomValue == 6)
tag = "NewRoom";
if (intRandomValue == 6)
SpawnPos += new Vector3(0, 0, -5);
if (intRandomValue == 6)
tag = "Room";
}
void OnTriggerEnter(Collider other)
{
if (CompareTag("NewRoom"))
{
Destroy(gameObject);
}
}
}
please help thanks
I don’t really know anymore details I can add srry
erm uh filler content here bc word minimum
Gleep Glorp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
A few things:
tag = "Room";
sets the tag of the GameObject that is spawning the child objects, not the child object themselves. To be able to access this new child, we need to keep a reference to it, meaning that we need theif
statements to contain more than one line. It should instead be something like:
if (intRandomValue == 1)
{
GameObject cube = Instantiate(Cube, SpawnPos, Quaternion.identity);
cube.tag = "NewRoom";
SpawnPos += new Vector3(0, 5, 0);
cube.tag = "Room";
}
-
Now that we’ve put the logic into a single
if
statement, you can see how everycube
will end up with the tag “Room”, since you overwrite it at the end. You should only set thetag
one time, with the proper value. -
The
OnTriggerEnter
‘sCompareTag
call checks against the tag of the current (spawner) object, not the object you’ve collided with. It’ll also delete the spawner object, not the room. You’ll instead want to check:
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("NewRoom"))
{
Destroy(other.gameObject);
}
}
OnTriggerEnter
will only get called if the spawner object has a (non-trigger) RigidBody and collides with another object that has a trigger collider. I doubt that your room spawner has a RigidBody, what did you actually want to achieve with thisOnTriggerEnter
callback?
1
you need to fix your OnTriggerEnter
function:
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("NewRoom"))
{
Destroy(other.gameObject);
}
}