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?
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 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.