hello i have a script
function Start () {
// Select the first weapon
SelectWeapon(0);
}
function Update () {
// Did the user press fire?
if (Input.GetButton ("Fire1"))
BroadcastMessage("Fire");
if (Input.GetKeyDown("1")) {
SelectWeapon(0);
}
else if (Input.GetKeyDown("2")) {
SelectWeapon(1);
}
else if (Input.GetKeyDown("3")) {
SelectWeapon(2);
}
}
function SelectWeapon (index : int) {
for (var i=0;i<transform.childCount;i++) {
// Activate the selected weapon
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
So i have this script for 3 differents gun ( 2 machine gun and 1 sniper ) and all shoots in the same time so i want to disable the 2machine gun when i switch to 1Sniper.
Thanks !
so this is much better code here (not perfect for example the repeated firecomponent =…
Gameobject Weapon;
ScriptWithFireFunction fireComponent;
void Start()
{
Weapon = new GameObject();
fireComponent = new ScriptWithFireFunction();
}
if (Input.GetKeyDown("1")) {
Weapon = tranform.GetChild(0).gameobject;
fireComponent = Weapon.getcomponent<ScriptWithFireFunciton>();
}
else if (Input.GetKeyDown("2")) {
Weapon = tranform.GetChild(1).gameobject;
fireComponent = Weapon.getcomponent<ScriptWithFireFunciton>()
}
else if (Input.GetKeyDown("3")) {
Weapon = tranform.GetChild(2).gameobject;
fireComponent = Weapon.getcomponent<ScriptWithFireFunciton>()
}
if (Input.GetButton ("Fire1"))
Firecomponent.Fire();
im not sure why yours isnt working my guess is broadcast message might work even on a disabled object but it doesn’t matter because
you should try your hardest to never ever use sendmessage or broadcastmessage
A. They use reflection so there really intensive as far as CPU load.
B. you shouldnt call functions by string because if you rename that function and you do something alot do which is don’t require receiver you can basically just have a fire function decide to rename it Fire or Shoot or FireAndReload or something and suddenly Fire doesn’t exist but you get no error because your specifically allowed to call non existant functions. This is bad and it’ll be ages tracking down the stupid bug.
You need to do your best to make sure when you screw up its caught by the compiler and it throws a huge blaring alarm in your face saying this is wrong. Not tell it to ignore errors and be ambigous in coding.
Also you’ll notice i got the firecomponent i did not for example do
if (Input.GetButton ("Fire1"))
Weapon.getcomponent<ScriptWithFireFunciton>().fire();
which will work BUT
getcomponent is also really intensive you’d never ever want to use a getcomponent call with something that happens as often as firing a gun so you do it once on weaponswitch then save that reference so you dont have to keep finding it and call it from reference.
so don’t use sendmessage or broadcast message
and as much as possible never use getcomponent in an update loop or for soemthing that happens alot of times possibly numerous times per frame.