Home > Back-end >  Collison Not working with an empty object (unity)
Collison Not working with an empty object (unity)

Time:09-07

I wrote a Script which creates a array of gameobjects I added the rigidbody and boxcollider to the gameobjects.

I created an empty object and added the Script.

Unfortnetley the collison wont be detected.

If i add the Script e.g to my player then it works.

The code of the script of my gameobjects with the array:

for(int i= 0; i < rain.Length; i  ){
 rain[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
rain[i].name = "rain"   i;
rain[i].AddComponent<Rigidbody>();
rain[i].AddComponent<BoxCollider>();
rain[i].transform.localPosition = new Vector3(0,15, 0);           
}

what i want to reach is. I have a floor which contains my character. As the (gameobjects) are falling and collide with the floor the should respawn....

but with the method onCollsionEnter(....); the method just refers to the empty object and not the individual gameobjects which I created.

CodePudding user response:

What object did you attach the OnCollisionEnter script to? I have attached OnCollisionEnter script to the plane, and it is just working fine.

enter image description here

I added this collision script to plane

Just like you, I attached this script to an empty gameobject

It would be helpful if you could clarify your question if I misunderstood it

CodePudding user response:

I just attached my Rain Script with the of array of gameobjects to my floor object.

And in the Floor Script. I created a Rain array and linked to my Rain Script.

Floor Script: Gameobject[] rains;

In Start() rains = GetComponent().Rain;

I created the OnCollisonEnter() Method

And wrote if(collison.gameobject.equals(rains[i]){ }

My main Idea was to attach the Script to an empty object and do the collison stuff there. Like If rains[i] collide with Player etc.

I Hope Somone can Help me out

  • Related