- Home /
How To Disable A Prefab?
Hello,
I have a GUI click and drag - basically, you can click on a GUI button of, say a turret and a turret prefab is initiated on the mouse position and you can drag it onto the scene somewhere - the problem is, is when I drag it around the scene, its already active - in other words, its shooting the enemy.
I want it to be active when the user clicks (places the turret) - so is there a way to deactivate all the scripts on that particular prefab, and then activate it when the user clicks?
function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; var layerMask = 1 << 8; if (chooseLocation) { layerMask = ~layerMask; Physics.Raycast (ray, hit, 500, layerMask);
currentObj.Translate((Vector3(hit.point.x, currentObj.position.y, hit.point.z) - currentObj.position) * Time.deltaTime *followSpeed);
}
if (Input.GetButtonDown("Fire1")){ //Input.GetMouseButton(0)
chooseLocation = false;
}
// Cancel Selection \\
if(Input.GetButtonDown("Fire2") && currentObj.gameObject){
objLimit[currentSelected] ++;
chooseLocation = false;
Destroy(currentObj.gameObject);
}
}
function OnGUI () {
selGridInt = 0;
if(chooseLocation == false){
if (GUI.Button(Rect(10, 10, 120, 30), objItemNames[selGridInt] + " ("+objLimit[selGridInt]+")") && objLimit[selGridInt] > 0){
currentSelected = selGridInt;
chooseLocation = true;
objLimit[selGridInt] --;
currentObj = Instantiate (objGroup[selGridInt],Vector3(0,0,0),Quaternion.identity);
currentObj.gameObject.GetComponent("BoxCollider");
currentObj.position.y = groundPlane.position.y + 0.5;
}
}
Thanks
Answer by efge · Mar 31, 2011 at 02:20 PM
You could use Input.GetButtonUp to set an boolean variable true and create an "overall" if-statement in the function Update for your turret script:
if (Input.GetButtonUp(...))
isReady = true;
and
function Update () {
if (isReady) {
...
}
}
Or you could disable/enable that script like any other component:
var script : ScriptName;
script = GetComponent("ScriptName");
...
if (Input.GetButtonUp(...))
script.enabled = true;
Answer by e-bonneville · Mar 31, 2011 at 02:21 PM
Well, you just need to set a boolean variable (true/false variable) to true when you place the turret down. You can use an if statement to check if the variable is false, then make it so the turret won't shoot if it is. You don't need to completely disable the script.
Your answer
Follow this Question
Related Questions
Activating & Deactivating prefabs from a prefab pool VS simply moving them on and off-camera (iOS) 1 Answer
Unchecking a prefab's script in the Inspector doesn't "deactivate" it? 1 Answer
How to activate and deactivate a gameObject 1 Answer
How to activate/deactivate a component(Animator) when a specific game object was activated 0 Answers