- Home /
Edit - Code isolation using random ID numbers C#
void OnTriggerEnter(Collider otherParticle){
//makes sure the code only happens when you collide with another prefab sphere.
if (otherParticle.gameObject.tag == "Proton") {
//add the mass of this sphere to the other sphere
otherParticle.rigidbody.mass = rigidbody.mass + otherParticle.rigidbody.mass;
//destroy this sphere leaving only the other sphere containing the combined mass
Destroy (gameObject);
}
}
I have these spheres. These spheres are programmed to gravitate towards one another. On the collision I want to add the mass of them together and the result be a single sphere. I'm still also trying to figure out the radius increasing upon combining them. Help with that, too, would be sweet!
To elaborate, this script is applied to every sphere I have in the scene. My issue is with the fact that upon collision both spheres are destroyed. I understand the code is being ran on both objects because the sphere collider trigger is the same radius on both of them so both spheres issue the code upon collision. I have to tried reduce the size of the sphere collider set as a trigger and the regular sphere collider, so that they are contained within the mesh of the sphere. Using the transport controls for the colliders, I set them apart from each other within the sphere mesh. I was hoping that if the collider on one entered only the collider trigger on the other then only one of them would be destroyed, but it seems that OnTriggerEnter issues two different ways. When this game objects collider enters another gameobjects collider trigger and also when this gameobjects collider trigger is entered into by another gameobjects collider.
I need help isolating this code to only one of the spheres! PLZ :)
Why don't you make a manager script where it would decide who gets to be destroyed as well as alter its size and mass?
Try Destroy(otherParticle.gameObject)
as it will destroy the other and it wouldn't be able to destroy this I mean the faster will remain the script who executes first will win
I have tried otherParticle.gameobject and the problem persists because the otherParticle is still also trying to delete this particle that's trying to delete the other particle lol... How would I go about crafting a manager script? I was thinking of something along the lines of a separate gameobject that handled that, but I don't know how to reference a collision between two objects from another gameobject/script. Do You?
Answer by Kiwasi · Aug 28, 2014 at 09:22 AM
Manager pseudo code as follows
// On the particle
ParticleManager.DestroyMe (gameObject, otherGameObject);
// The Manager
public void DestroyMe (GameObject go1, GameObject go2){
if (go1 && go2){
Destroy (go1);
}
}
An alternate solution without the manager would be to use unique IDs. I've used a random number, but you could also use a static int to generate the ID. Pseudo code:
public float ID = Random.Range(0f,10f);
// Inside the collision code
if(ID > other.ID){
Destroy(gameObject);
}
using UnityEngine;
using System.Collections;
public class Protons : $$anonymous$$onoBehaviour {
private float ID;
private Protons otherProton;
void Update(){
ID = Random.Range(0f,10f);
}
void On$$anonymous$$ouseDown(){
Destroy(gameObject);
}
void OnTriggerEnter(Collider otherParticle){
if(otherParticle.gameObject.tag == "Proton" && gameObject.tag == "Proton"){
otherProton = otherParticle.GetComponent<Protons> ();
if(ID > otherProton.ID){
rigidbody.mass=rigidbody.mass+otherParticle.rigidbody.mass;
Destroy(otherParticle.gameObject);
}
}
}
}
Thanks so much for the help! The ID's work like a charm!
Your answer
Follow this Question
Related Questions
Randomly keeping paintball intact on collision 1 Answer
Creating a Asteroid health script 5 Answers
Destroy bullet on random collision 2 Answers
Collision, but then only affect ONE of the gameObjects? 2 Answers
2 objects collide, need to destroy one 2 Answers