Hi. Standard introduction in that I’m quite new to Unity and just trying to make a few simple bits and pieces to try and familiarise myself with it more. I’m hitting a bit of an issue that I can’t quite resolve.
I have a series of floating objects which just have box colliders attached to them. After a certain amount of time they move randomly for a few moments before resetting to their original position. After that point they can be clicked on and other actions happen as needed.
The plan that I’ve had was to attach a rigidbody to each object and then to fire it in one of four random directions (up, down, left, right) and let gravity handle the rest, which is working fine and suits my needs:
Obj[rb].gameObject.AddComponent(Rigidbody2D);
Obj[rb].GetComponent(Check).FireOff(RandomDirection);
Obj is the collection of my objects and Check is the script which handles the other elements.
Then in check the FireOff function calls:
gameObject.rigidbody2D.AddForce(transform.right*1000); (or whichever direction as relevant)
However, when it comes to resetting the objects, they will position fine and I can remove the rigidbody with the following:
Destroy(this.gameObject.rigidbody2D);
transform.position = MyPos;
transform.rotation.z = Vector3.up.z;
But when doing this it loses the click event coming through from the BoxCollider. In all other circumstances I’ve found it works just fine, but it seems that adding and removing the rigidbody causes it to stop responding entirely.
Does anyone have any ideas as to what would be causing this? I’ve been searching and playing and testing for about two days now and I’ve found nothing that might be causing it to drop off. And if I monitor one of the objects through inspector while running it does exactly what you would expect (the rigidbody temporarily attaches and then removes).
Is there some elementary lesson that I’ve missed somewhere that anyone could possibly help out with?
Thanks in advance