x


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;
         }
        }
more ▼

asked Aug 02 '12 at 09:03 PM

thenickhis gravatar image

thenickhis
0 3 5

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.

Aug 02 '12 at 09:19 PM fafase

Thanks for the comment, that would work if there wasn't other players in the game. I updated my question with the scripts.

Aug 02 '12 at 09:53 PM thenickhis
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Set all meshRenderer of children false and show only the ones that need to be seen.

more ▼

answered Aug 02 '12 at 10:25 PM

Sundar gravatar image

Sundar
581 2 2

Thank you! I have to overhaul my script, but it seems to be working better this way.

Aug 02 '12 at 11:04 PM thenickhis
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2077
x408
x79
x31
x13

asked: Aug 02 '12 at 09:03 PM

Seen: 486 times

Last Updated: Aug 02 '12 at 11:04 PM