- Home /
2D Colliders and spawners check
I'm have no idea what can I may missing for this setting:
have 2 sprites, each one with the respective 2d colliders and 2d rigid bodies. when sprite A goes to sprite B, sprite B will be deleted, to do so I have attached on sprite B a script with the following:
using UnityEngine;
using System.Collections;
public class Interaction: MonoBehaviour {
void OnCollisionEnter2D(){
Destroy (this);
}
}
but nothing happens, put a debug before destroy but didn't display it, also put sprite A and B on different layers and setup it on project setting-> physic and still nothing, what I'm missing??
Now with the spawner I want:
I have an empty object that have 5 child empty object those one are my spawners. each one create a box, when one of the boxes moves from it original postion the spawner will generate a new box. At first I wanted to have a null checker but I failed. this was what I had:
using UnityEngine;
using System.Collections;
public class Spawn: MonoBehaviour {
public GameObject Box;
void Updated (){
if (Box = null){
spawn();
}
}
void Awake(){
Spawn();
}
void Spawn(){
GameObject newBox = Instantiate(Box, transform.position, transform.rotation) as GameObject;
newBox.name = "Box";
}
It created the awake box, but later on do nothing else, I put the prefab but after create the awake box it box is none in the inspevtor.
also tried colliders for the spanwer like this:
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
public GameObject box;
public int boxnum = 0;
void OnCollisionExit2D () {
if (boxnum == 0){
Spawn();
}
}
void Awake() {
Spawn ();
}
void Spawn(){
GameObject newbox = Instantiate(box, transform.position, transform.rotation) as GameObject;
newbox.name = "box";
boxnum++;
}
}
all good but one detail is that this script goes to each spawner and I have another script that getcomponent and call for boxnum and add, so the problem is that it only send the information to only one spawner, as a mere of fact the first on the inspector and doing so alter the result, If the box of the 1st spawner moves generates the other box as desire, but if a box of the other 4 spawner moves the count of the 1st inspector is the one the get altered instead of the respective spawner so the spawner doesn't create a new box, What can I do??
Your collisions are incorrect, they take an argument of type Collision2D, this is clearly stated in the documentation. The method signatures must match for unity to make the call.
yeah!!! totally forgot about Collision2d on the void, THAN$$anonymous$$S man.
Half problem solve, a quick note for the spawners if I try gameobject.findwithtag("name"). should I try getcomponent in plurar or singular so the script goes to every objects related or will do no difference?
Your answer
![](https://koobas.hobune.stream/wayback/20220613172441im_/https://answers.unity.com/themes/thub/images/avi.jpg)