- Home /
Activating gameObject issue...
I have a gameobject called weaponPrimary and one called weaponSecondary, so the player can have one out or both out at the same time. All the possible weapons are childed under both of them, having only one active according to the player's choice on the loadout menu. By pressing 1, weaponPrimary either activates or deactivates, and same for weaponSecondary with pressing 2. weaponPrimary/Secondary have a weaponmanager script that keeps track of which child should be active by assigning the child to the "equipped" variable. This is all working fine except for one thing: Whenever weaponPrimary/Secondary reactivates, ALL the children reactivate with it for a split-second and then disappear, leaving the equipped weapon active. This looks bad, as a large mass of weapons flicker on and off every time. I use SetActiveRecursively for deactivate for switching weaponPrimary/Secondary and weapons off, but I only reactivate the weaponPrimary/Secondary and the child that is set as equipped. Any help on this would be appreciated.
Here is the previously mentioned weaponmanager script on weaponPrimary/Secondary: var rifle : Transform; var pistol : Transform; var armlaser : Transform; var armemp : Transform; var equipped : Transform;
function Update () {
if (rifle == equipped) {
rifle.gameObject.SetActiveRecursively(true);
} else
{
rifle.gameObject.SetActiveRecursively(false);
}
if (pistol == equipped) {
pistol.gameObject.SetActiveRecursively(true);
} else
{
pistol.gameObject.SetActiveRecursively(false);
}
if (armlaser == equipped) {
armlaser.gameObject.SetActiveRecursively(true);
} else
{
armlaser.gameObject.SetActiveRecursively(false);
}
if (armemp == equipped) {
armemp.gameObject.SetActiveRecursively(true);
} else
{
armemp.gameObject.SetActiveRecursively(false);
}
}
and here is the excerpt of the script that determines the button pressing:
if (primaryout)
{
primaryWeapon.gameObject.active = true;
primaryWeapon.gameObject.GetComponent("weaponmanager").equipped.gameObject.SetActiveRecursively(true);
} else
{
primaryWeapon.gameObject.SetActiveRecursively(false);
}
if (Input.GetButtonDown("Draw primary weapon"))
{
if (primaryout)
{
primaryout = false;
alreadyCheckedprimary = true;
}
if (!primaryout && !alreadyCheckedprimary)
{
primaryout = true;
}
}
if (secondaryout)
{
secondaryWeapon.gameObject.active = true;
secondaryWeapon.gameObject.GetComponent("weaponmanager").equipped.gameObject.SetActiveRecursively(true);
} else
{
secondaryWeapon.gameObject.SetActiveRecursively(false);
}
if (Input.GetButtonDown("Draw secondary weapon"))
{
if (secondaryout)
{
secondaryout = false;
alreadyCheckedsecondary = true;
}
if (!secondaryout && !alreadyCheckedsecondary)
{
secondaryout = true;
}
}
You should post your code. There is no way for us to find out what is wrong just by telling us even though you give a lot of details.
Your solution in the meanwhile could be to have an animation that hides the weapon and then switch it. When changing the animation takes the weapon out of camera view and comes back with a different weapon.
Thanks for the comment, that would work if there wasn't other players in the game. I updated my question with the scripts.
Answer by Sundar · Aug 02, 2012 at 10:25 PM
Set all meshRenderer of children false and show only the ones that need to be seen.
Thank you! I have to overhaul my script, but it seems to be working better this way.