- Home /
Help with getting a bool from another script
Okay so I am trying to make a script reference, however the game object the script is attached to is being spawned by a wave spawner using the prefab of it. The game object that I have the other script under is attached to a game object which is attached to the game object carrying the script I am trying to reference to. There are many enemies being spawned and I need my script to only deal with the one it is under. This is my current script and I am trying to check for the bool "dead" from the other script.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TurnOffLight : MonoBehaviour {
public GameObject EYE;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (GameObject.Find("ENEMY").GetComponent<Enemy>().dead)
{
Destroy(EYE);
}
}
} Any help would be appreciated, I know my description at the top is a bit fuzzy it is kinda hard to explain if you have any questions about what I mean please ask.
Answer by smkmth · Nov 12, 2017 at 11:34 AM
I am not sure what you mean by 'I need my script to only deal with the one it is under' - so finding it a little difficult to help you. If i were trying to access one or a number of prefabs which have spawned in a scene i would use the GameObject.FindGameObjectsWithTag - the code example here might be relevant? i could use more details if you still need help ?https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
@smkmth So when I mean the one it is under I have many enemies that are all the same prefab being spawned, each enemy has 2 game objects under them the body and the glowing eye I am trying to make it so when you shoot it the eye object disappears but the script with the "dead" boolean is under is the body not the eye. When you shoot an enemy it will return the value "dead" as true but I want the eye to have an if statement that will remove the eye if "dead" is true. I don't however want to use a tag because all the enemies have the same tag and I don't want when you kill one all the enemies' eyes disappear. Hopefully this makes more
Answer by SnailGirl · Nov 12, 2017 at 12:05 PM
Not completely sure what you want to do, but I think you want to check if the previously instanciated object is "dead" right? Perhaps it would be easier to have an index of the objects, if it is always the previously created Gameobject you want to reach (as in "the one it is under"?).
Make a list and add all your instanciated objects to it. Keep the list in a static script, like a gameControl script or something
(for the control script)
Public List enemies = new List();
(for the enemy script)
int PrevEnemyCount;
void Start()
{
enemies.Add(gameobject)
PrevEnemyCount = enemies.Count -1;
}
void Update()
{
if (enemies[PrevEnemyCount].GetComponent().dead)
{
// whatever needs to be done
}
}
I'm not sure this is the best way to do this as I never tested this out, but I think it's better to think about it like this anyway :) good luck!
@BlackSilkStockings What I am trying to do is I have many enemies that are all the same prefab being spawned, each enemy has 2 game objects under them the body and the glowing eye I am trying to make it so when you shoot it the eye object disappears but the script with the "dead" boolean is under is the body not the eye. When you shoot an enemy it will return the value "dead" as true but I want the eye to have an if statement that will remove the eye if "dead" is true. I don't however want to use a tag because all the enemies have the same tag and I don't want when you kill one all the enemies' eyes disappear.
If I understood correctly, the eye is a child of the enemy? in that case you can check the parent. The parent is under the transform (gameobject.transform.parent). Check if the dead is on with something like GetComponentsInParent: https://docs.unity3d.com/ScriptReference/GameObject.GetComponentsInParent.html
I would make an int counter or something, to see how many hits the enemy has gotten, and then set the component from the parent to the children, so you don't have to have the check constantly in the update function. that way what happens to the enemy can affect the children (aka eyes)
so ins$$anonymous$$d of checking a bool, something like this
//for each hit on the enemy, ++ the hit
int hit;
//if it has been hit more than twice, you destroy the first child, which is the first eye
if (hit>2)
{
Destroy (transform.GetChild(0).gameObject);
}
//if more than 4, destroy the second eye
if (hit>4)
{
Destroy (transform.GetChild(1).gameObject);
}