- Home /
How to set a specific gameobject in an array false when it collides with a trigger.
I have the following code that returns all of the basketballs once all of the balls have entered a trigger. the problem i have is that i would like to set the specific balls that have already interacted with the collider false until the score = 15 i.e. the return to position part. I don't want the balls that have already collided with my trigger to just float in space continuously while the others are thrown. My basketball array is set to 15 publicly in the inspector and I have all of my elements populated in the inspector.they are not instantiated. they are all ingame gameobjects and i can set them false individually i.e.
if (other.gameObject.CompareTag("basketball") ) {
basketball[1].SetActive (false);
}
but this sets a specific ball inactive. I would like to set "the" specific ball that interacted with the collider false without writing a bunch of if statements.
code is as follows:
public class returnballsreturnscore : MonoBehaviour {
public GameObject[] basketball;
private Vector3[] oldPositions;
Transform[] models;
Rigidbody[] rigid;
public int score;
// Start is called before the first frame update
private void Awake()
{
GameObject[] tempModels = GameObject.FindGameObjectsWithTag("basketball");
oldPositions = new Vector3[tempModels.Length];
models = new Transform[tempModels.Length];
rigid = new Rigidbody[basketball.Length];
score = 0;
for (int i = 0; i < tempModels.Length; i++)
{
models[i] = tempModels[i].GetComponent<Transform>();
rigid[i] = basketball[i].GetComponent<Rigidbody>();
oldPositions[i] = models[i].position;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("basketball") )
{
score++;
if (score == 15)
{
for (int i = 0; i < models.Length; i++)
{
models[i].position = oldPositions[i];
rigid[i].isKinematic = true;
rigid[i].isKinematic = false;
score = 0;
}
}
}
}
}
Answer by Llama_w_2Ls · Aug 12, 2020 at 06:48 PM
Well you can find the specific object that collided with the trigger and deactivate it by writing: public float Score;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Basketball"))
{
other.gameObject.SetActive(false);
Score++;
}
}
Your answer
Follow this Question
Related Questions
Array index help. 1 Answer
Remove object from List after gameObject was destroyed in between Scene loads 1 Answer
C# array not behaving as expected 0 Answers
Error CS0029 Help? (Screenshot of Exact Error) 1 Answer
Multiple Cars not working 1 Answer