- Home /
Make Script Disable
Hello ..
I have one script which is repeatedly instantiating game object..
But after some condition match, i want to disable that script along with that object..
Script Code :
#pragma strict
public var enemy1Prefab : GameObject;
private var x : float;
private var y : float;
private var z : float;
private var scoreControllerScript : ScoreController;
private var gameControllerScript : GameController;
function Start () {
gameControllerScript = GameObject.Find("Main Camera").GetComponent("GameController");
scoreControllerScript = GameObject.Find("Main Camera").GetComponent("ScoreController");
InvokeRepeating("CreateRandomShip",0.01f,1.0f);
}
function Update () {
}
function CreateRandomShip()
{
x = Random.Range(-4.00f , 4.00f);
z = -6.72f;
y = 8.0f;
enemy1Prefab.transform.position = new Vector3(x , y , z);
Instantiate(enemy1Prefab,enemy1Prefab.transform.position,enemy1Prefab.transform.rotation);
if(gameControllerScript.gameStarted)
scoreControllerScript.ManageEnimies();
}
And Code which i have written to Disable Object and script is here :
if(GameObject.Find("Level6").active)
{
Debug.Log("level 6");
enemy1CreationScript = GameObject.Find("Level6").GetComponent("Enemy1Creation");
enemy1CreationScript.enabled = false;
GameObject.Find("Level6").active = !GameObject.Find("Level6").active;
}
By this code., My object and script both become disabled. I have Checked in Inspector panel..but then also it continuous to generate game object..
So what can be the reason behind this? Please Help me to solve out this issue..
Thanks for Your Support and Help..
Answer by pl_bota95 · May 17, 2013 at 07:44 AM
Use GameObject.Find("your object").SetActive(true/false).
Answer by Dreamora · May 17, 2013 at 09:11 AM
Enabling or disabling scripts only impacts the automatic continous functions: Start, Update, LateUpdate, FixedUpdate
All the rest will continue to work independent on if the script is active or not. To disable that you will need to disable the script.gameObject.SetActive(...)
if setactive mentions its not allowed, you are on Unity 3.x and not 4x in which case its just .active
@Dreamora sir.. have used your idea.. but still it continuous to generate game object.. Now What Shoild i do ?? :(