- Home /
how to activate/de-activate different scripts on a gameobject?
Hi all,
I am trying to learn unity and some scripting on the way. I have a ship with a shoot script on it. What I can't figure out is how to (when I collide with a powerup) to turn off the shoot script and activate say a laser script (shoot script with different bullet prefab)
I am currently doing this by having 2 seperate prefabs each with the different shoot scripts on that instantiate on a "mount" when the power up is collided with (this seems to work but not all the time).
It would be easier if i could have the 2 scripts on my ship and when i hit the power up it turns the basic gun script off and turns the laser script on. Can anyone help me with this please?
Answer by programmrzinc · Apr 17, 2012 at 10:06 PM
var HOLDER : GameObject; //Holds BOTH scripts
function //Whatever () {
HOLDER.GetComponent(//Name Of Script 1).Enabled = False or True;
HOLDER.GetComponent(//Name Of Second Script).Enabled = False or True;
}
You have to tweak it to your specifications. Sorry:)_
I've tried this (will add to my ship object):
var HOLDER : GameObject; //Holds BOTH scripts
function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "spiral_laser_powerup") {
HOLDER.GetComponent(Shootspherescript).Enabled = False;
HOLDER.GetComponent(Shoot_spiral_laser_script).Enabled = True;
} }
I'm getting the error "BCE005: $$anonymous$$ identifier: 'False'. any ideas?
now i'm confused hehe, is it something i need to add under the first line of code? eg False=False and True=True ??
Sorry should have made it more clear. You dont need a capital letter for false. False and false are totaly different things.
something.enabled = false; something.enabled = true;
I was actually going to write a script out to help you but for some reason unity just doesn't want to work today. Had to restart it a dozen times might reinstall it.
Answer by Fabkins · Apr 18, 2012 at 12:32 AM
I would use a different method. I would attach the multiple scripts to the object. Each script would have the same function calls, in this case "fire".
From the parent script:
function Update () {
// do your logic....
gameObject.SendMessage("fire","laser");
gameObject.SendMessage("fire","blaster");
}
And then each script can check to see if its being called. Like so:
function fire(type: String)
{
if(type=="laser")
{
Debug.Log("Fire weapon laser");
}
}
The handy thing about do this rather than turning the script off is that if the weapon has a cooldown timer then the script can carry on doing its thing.
I now have this script (see below). I am getting this error: $$anonymous$$issingFieldException: Field 'Shootspherescript.Enabled' not found.
var HOLDER : GameObject; //Holds BOTH scripts var orb_mount : GameObject; //object to spawn item at var orbs : GameObject; //powerup spawned
function Start () {
HOLDER.GetComponent(Shootspherescript).Enabled = true;
HOLDER.GetComponent(Shoot_spiral_laser_script).Enabled = false;
}
function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "spiral_laser_powerup") {
HOLDER.GetComponent(Shootspherescript).Enabled = false; <-error
HOLDER.GetComponent(Shoot_spiral_laser_script).Enabled = true;
}
if (other.GameObject == "orb_power_up")
{
var orbs : GameObject = Instantiate(orbs, orb_mount.transform.position, orb_mount.transform.rotation);
orbs.transform.parent = orb_mount.transform;
}
}
Try:
HOLDER.GetComponent(Shootspherescript).enabled = false;
Lower case enabled.
thanks for everyone's help :)
my working script:
var HOLDER : GameObject; //Holds BOTH scripts
function Start () {
HOLDER.GetComponent(Shootspherescript).enabled = true;
HOLDER.GetComponent(Shoot_spiral_laser_script).enabled = false;
}
function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "spiral_laser_powerup") {
HOLDER.GetComponent(Shootspherescript).enabled = false;
HOLDER.GetComponent(Shoot_spiral_laser_script).enabled = true;
yield WaitForSeconds (0.001);
Destroy(other.gameObject);
}
}
Your answer
Follow this Question
Related Questions
Activating/Deactivating GameObject Problem :( 3 Answers
Activating things- What on earth am I doing wrong??? 2 Answers
How to reset a variable when activated 1 Answer
Deactivating and Activating a group and all it's children? 1 Answer
How to deactivate gameObjects instead of destroying them ? 1 Answer