I have an array on gameobjects and when i collide with an gameobject, i would like to enable an script of the next gameobject on the array. How can i do that? Something like array on cubes → collide to cube1 → enable script on cube2 and so on.
I think for this there will be several steps:
- You need to find the index of the colliding gameobject in the array.
- Get the index + 1 element in the array.
- Once you successfully get that element, then you can enable the script by GetComponent method.
I’d let the objects that you have in your array call a method on the Component that contains the array which then calls the method of the next object in the array. So your scripts should look sth. like this:
// The object inside the array (ArrayElementScript)
public var arrayContainer : ArrayContainerScript;
function OnCollisionEnter() {
arrayContainer.CollisionDetected(gameObject);
}
function DoYourThing() {
// do whatever you want to do with the next object in the array
}
// The script containing the array (ArrayContainerScript)
public var TheArray : GameObject[];
public function CollisionDetected(go : GameObject) {
for(var i : int = 0; i < TheArray.length; i++) {
if(TheArray[i] == go) {
TheArray[i + 1].GetComponent.<ArrayElementScript>().DoYourThing();
}
}
}
Thanks for your reply’s. I forgot to say i’m coding in C#, but maybe i can get some direction from your javascript.
There your go (hope it works)
// The object inside the array (ArrayElementScript)
using UnityEngine;
using System.Collections;
public class ArrayElementScript : MonoBehaviour {
public ArrayContainerScript arrayContainer;
private void OnCollisionEnter() {
arrayContainer.CollisionDetected(gameObject);
}
public void DoYourThing() {
// do whatever you want to do with the next object in the array
}
}
// The script containing the array (ArrayContainerScript)
using UnityEngine;
using System.Collections;
public class ArrayContainerScript : MonoBehaviour {
public GameObject[] TheArray;
public void CollisionDetected(GameObject go) {
for(int i = 0; i < TheArray.length; i++) {
if(TheArray[i] == go) {
TheArray[i + 1].GetComponent<ArrayElementScript>().DoYourThing();
}
}
}
}
It’s kind of working now. I got it working that when collision on gameobject (bomb as in bomb_0, bomb_1, bomb_2…) is detected, it destroys the object and then enables animation script (BombBurn.cs) on the next bomb (collision on bomb_0 → start bomb_1 animation) on the array. Now i just need somehow to check that if any other BombBurn is allready enabled that it would not enable another. So that only one animation is enabled at the time. As you may noticed, i’m n00b to programming and unity. But i’ll get there eventually ![]()
public GameObject[] BombArray;
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "Bombs") {
Destroy (col.gameObject);
count += 200;
for(int i = 0; i < BombArray.Length; i++) {
if(col.gameObject == BombArray[i]) {
BombArray[i+1].GetComponent<BombBurn>().enabled = true;
}
}
}
}
In your BombBurn class call a method on your ArrayContainer that tells it if a BombBurn is currently in progress and only do the stuff inside OnCollisionEnter2D if there is currently no burning in progress
public GameObject[] BombArray;
private boolean burningInProgress = false;
void OnCollisionEnter2D(Collision2D col){
if(!burningInProgress)
if (col.gameObject.tag == "Bombs") {
Destroy (col.gameObject);
count += 200;
for(int i = 0; i < BombArray.Length; i++) {
if(col.gameObject == BombArray[i]) {
BombArray[i+1].GetComponent<BombBurn>().enabled = true;
BombArray[i+1].GetComponent<BombBurn>().arrayContainer = this;
}
}
}
}
}
public void SetBurningInProgress(boolean isInProgress) {
burningInProgress = isInProgress;
}
You give the BombBurn class the ArrayContainer class:
BombArray[i+1].GetComponent<BombBurn>().arrayContainer = this;
Then inside the BombBehaviour on start you call
arrayContainer.SetBurningInProgress(true);
And when the burning is over
arrayContainer.SetBurningInProgress(false);
If you have problems implementing this just ask ![]()
Didn’t quite understand that. My script was part of my PlayerController.cs script (not BombBurn.cs), so i’m not sure how to check inside that (PlayerController) if BombBurn.cs is enabled in one of the Bombs gameobject…