Hi everyone
So I have an idea for a game that requires multiple instances of an object in the scene that react to each other. I’ve spent the past three weeks trying to get it to work, but so far, it’s obvious to me that my logic in making this part of the game is flawed, so I’m reaching out to you all to pick your brains for other ideas in how I may tackle this problem.
I don’t want to give the game away, so hopefully the following will be enough for you to see what I’m trying to accomplish.
So let’s start by saying first imagine a scene with many of the same enemy (prefab). They all share the same attributes (sprite/animations etc) and all act in the same way. Essentially, if one enemy comes in contact with another enemy it makes that enemy “active” and causes it to animate. If that enemy is in range of another enemy, it causes that one to become active and so forth - a chain reaction of sorts. Also, it’s important for me to know which enemy in the scene made an enemy active, so the whole process can become reversed. e.g. If an enemy becomes inactive, then any enemy that IT had made active originally, also becomes inactive. So recording the “parent” of the enemy is important. (the parent being the enemy that made this enemy active).
I’ve been using trigger enter to trigger when an enemy becomes active - essentially if trigger activates, it looks at that enemy’s script (they all have the same script on them in the prefab) to see if active is true, and if it is, become active itself and adds the name of that enemy to its parents list.
Now this above works fine on a one-on-one sense. But when the game first starts, it doesn’t. When the game loads, it will be loading the enemies, some of which will be connected in the above manner, and thus they need to present as active or otherwise using the method above.
To give an idea of what type of code of attempted, here’s a sampler:
This script is on each enemy prefab. (i.e. is the same on every enemy)
public class EnemyCore : MonoBehaviour (
public string thisEnemyID;
public bool enemyActive;
public bool firstEnemy;
public GameObject enemyOnState;
public List<string> enemyParent;
public List<EnemyCore> ConnectingScripts;
public EnemyMaster Master;
void Start()
{
thisEnemyID = gameObject.name.ToString ();
if (!firstEnemy)
{
enemyActive = false;
enemyOnState.SetActive(false);
}
Master = GameObject.Find ("GameMaster").GetComponent ("EnemyMaster") as EnemyMaster;
}
void OnTriggerEnter(Collider col)
{
//This looks to see if any enemy are connected at start-up, or become connecting during game
EnemyCore temp = col.gameObject.GetComponent ("EnemyCore") as EnemyCore;
if (!ConnectingScripts.Contains(temp))
{
ConnectingScripts.Add(temp);
}
Master.NextActivation(thisEnemyID);
//"NextActivation" above is in a central script (not on the enemies) that then broadcasts to all enemies "ActivateEnemy", received by the below by each enemy.....
}
void ActivateEnemy(string ID)
{
foreach( EnemyCore x in ConnectingScripts)
{
if (ID == thisEnemyID)
{
if(x.enemyActive)
{
if(!enemyParent.Contains (x.thisEnemyID))
{
enemyParent.Add(x.thisEnemyID);
enemyActive = true;
enemyOnState.SetActive(true);
}
}
}
}
}
}
Edit: Also note that one enemy may trigger more than one enemy and thus become the parent of multiple enemies, and vise versa - two enemies may trigger one enemy and that enemy would have two parents. So if one of those parents should become inactive, it would still be active if it’s other parent is still active!
Please ask any questions if anything above seems mirky. Really appreciate any and all suggestions or advice on this!
Thanks all