- Home /
Controlling a specific instance of a GameObject clone or otherwise
Hi, I'm making a Space Invaders clone for a school project that must include the use of user-defined data structures. I chose a LinkedList and a Queue made up of a LinkedList. What I would like to be able to do is take the last five enemies in the linked list and set a boolean concerning whether they can fire or not. The reason I want the last five to shoot is because they are, in theory, the only ones with a "clear" shot, meaning they won't collide with enemies below them because there shouldn't be any. My problem is that when I instantiate, I have to cast my scripts into GameObjects(Or at least what I was taught), but since gameobject is the base class I can't get to the variable that says whether they can fire or not.
Answer by JimNotaro · Apr 25, 2016 at 10:07 AM
What I beleive you are asking is how to access a script on the enemies that has a variable defining if they can fire of not. There are several ways to do this the most effective of them is to use GetComponent to assess the script like this
YourGameObject.GetComponent<YourScript>.EnableFire = false
obviously you will need to have found the gameObject you were looking for first but this benefits from allowing the same line of code to work on all instances of a gameObject since they will all have the same script name.
You should also not be casting your scripts into gameObjects, they should be components attached to a gameObject aka the enemy gameObject you made.
Thank you! I'm still getting used to Unity and this helps!
Answer by fishbrainz · Apr 25, 2016 at 09:23 AM
Get the script you need on the enemy by calling GetComponent() on the enemy gameobjects you stored in your lists
EnemyScript _enemyScript = List[indexInForLoop].GetComponent<EnemyScript>();
_enemyScript.yourPublicBoolVariable //your "clear shot" bool variable or w/e
Your answer
Follow this Question
Related Questions
Ideal Dynamic Object Generation Technique 1 Answer
Instantiating camera crashes my pc 0 Answers
Not instantiating? 0 Answers