- Home /
Disable any objects through 1 script
Hi I made clones of a ghost around my map and they flicker over time but I want it so after 11-15 flickers that the ghost will disappear and it randomly picks a clone and enables that instead
GameObject ScaryFemale = GameObject.Find("ScaryFemale");
GameObject ScaryFemale1 = GameObject.Find("ScaryFemale1");
GameObject ScaryFemale2 = GameObject.Find("ScaryFemale2");
}
while (ScareSel <= 1){
if(invisloop <= 11) {
Debug.Log (invisloop);
if (gameObject.renderer.enabled) {
curTime = Random.Range(0.01f, maxTimeInvis);
} else {
curTime = Random.Range(0.01f, maxTimeVis);
invisloop++;
}
gameObject.renderer.enabled = !gameObject.renderer.enabled;
yield return new WaitForSeconds(curTime);
}
There are 3 objects so how do I get the GameObject "ScaryFemale" in this code instead of the gameObject.renderer.enabled?
I had this working as 4 scripts by placing 3 on the character and having 1 control which script was running but it really wasn't that feasible
Answer by Alessio89 · Aug 24, 2014 at 11:41 AM
As we can see from your snippet, it seems that the three GameObjects are inside some brackets, making them valid only inside those brackets. You should instead put them either outside any method and make them public, so you can access them from everywhere, or declare them outside those brackets (I don't know if there is this possibility, your snippet doesn't include much).
public class Script : MonoBehaviour
{
// I've delcared them outside any method. I can now access them from anywhere in this class.
GameObject ScaryFemale;
GameObject ScaryFemale2;
void Start()
{
// Let's fill in the objects when the game starts
ScaryFemale = GameObject.Find("ScaryFemale");
ScaryFemale2 = GameObject.Find("ScaryFemale2");
}
void Update()
{
//I Can now access both scaryfemales from here
ScaryFemale.transform.position = Vector3.zero;
ScaryFemale2.renderer.enabled = false;
}
}
(This code is just a sort of pseudo-code, you should not be copying this, instead look at what I meant and try to implement it in your own script :))
no you are completely right and I feel stupid now... I've been working on my code all night and I simply messed this one up, thanks for the help! :)
eheh, it happens to the best of us :) it can be tiresome at time to do these stuff! I'm glad you figured it out. Have fun with your poject! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613161412im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer 3 Answers
Load a GameObject that is outside of script and set it active at the same time 1 Answer
GameObject.setActive(bool) is not activating my gameObject 1 Answer
Networkly disabaling gameobject? 0 Answers