Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

.SetActive(false) changing rotation?

Discussion in 'Scripting' started by olliewe88, Feb 11, 2019.

  1. olliewe88

    olliewe88

    Joined:
    Dec 4, 2015
    Posts:
    11
    Hi, when I change weapons it deactivates the current one and activates the one you switch to and vise versa . However, the guns seem to change positions


    Code (CSharp):
    1.  
    2.         if (Input.GetKeyDown(KeyCode.Z))
    3.         {
    4.             active = true;
    5.             //weaponactivate1.SetActive(false);
    6.             //weaponactivate2.SetActive(true);
    7.         }
    8.         if (Input.GetKeyDown(KeyCode.X))
    9.         {
    10.             active = false;
    11.         }
    12.         if (active == true)
    13.         {
    14.         weaponactivate2.SetActive(false);
    15.         weaponactivate1.SetActive(true);
    16.         }
    17.         if (active == false)
    18.         {
    19.         weaponactivate1.SetActive(false);
    20.         weaponactivate2.SetActive(true);
    21.         }
    Code is above in the update
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    Does your weapons have any OnEnable / OnDisable in the scripts on them? If so, they will be called multiple times which may cause this.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Add some debugs to see if position or rotation is changing. What does the inspector show? Code wise, turning things on and off will not change position unless you have possibly some other code doing something. Remember that some stuff stops working when things are turned off.

    Also, look into some "else if" statements, it will help optimize code some and reduce chances of errors resulting from multiple ifs running.

    I would also say that you should turn stuff on and off within your input checks so it isn't being called repeatedly.
     
  4. olliewe88

    olliewe88

    Joined:
    Dec 4, 2015
    Posts:
    11
    The only thing that is changing is the rotation and I have checked all the scripts attached and none of them rotate the object. They also don't contain any OnEnable/OnDisable in the scripts. If I manually switch the gun active and not active via the tick box it doesn't rotate
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,294
    It's either the scripts, or an animator that changes the rotation.