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

Weapon Switching, is there an easy way?

Discussion in 'Scripting' started by RuanJacobz, Sep 27, 2015.

  1. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    My player is without a weapon by default. I want him to be able to pick up guns and switch guns.

    My first thought was to just make every single gun you can get, a child of his right forearm, and disable them. This way they are hidden in the background and their scripts wont run in the meantime. The game will patiently wait until you hit a pickup, then enable it.

    So I place this little code on my pickup, as a test:

    Code (CSharp):
    1.  private GameObject _rifle;
    2.  
    3.         public void OnCollisionEnter(Collision collision)
    4.         {
    5.             _rifle = GameObject.Find("Rifle");
    6.             _rifle.SetActive(true);
    7.         }
    Sadly, this doesn't work. Unity tells me: "Object reference not set to an instance of an object"

    It turns out this may be, because Unity can't find objects that are disabled. So now I'm a little lost about how to work around this issue. Can you guys help me out?

    UPDATE:

    I got it to work temporarily now with this script:
    Code (CSharp):
    1.         public GameObject WeaponGameObject; //The object you want to activate
    2.  
    3.         private void OnCollisionEnter(Collision collision)
    4.         {
    5.             WeaponGameObject.SetActive(true);
    6.         }
    7.     }
    But sadly I feel this is a temporary solution, because if the player is not in the scene when I was designing the level I can't actually place his weapon in the public game object slot.
     
    Last edited: Sep 27, 2015
  2. BonBonStudio

    BonBonStudio

    Joined:
    Mar 16, 2014
    Posts:
    3
    Instead to disable the whole GameObject you could only turn the mesh renderer off. Or set the guns active and then use:

    Code (CSharp):
    1. public GameObject[] Guns;
    2. // maybe set size in the inspector and drag all in, or:
    3. void initGuns() {
    4.    Gun = new GameObject[10];
    5.    string gBase = "gun_";
    6.    for(int i-0;i<10;i++) {
    7.      Gun[i] = GameObject.Find(gBase+i); // Ex: gun_0
    8.      Gun[i].active=false;
    9.    }
    10. }
    and to make the gameobjects active again, use : Gun[curGun].active=true;

    UPDATE:

    Things that can find inactive gameObjects :

    1. transform.Find() or transform.FindChild()
    2. transform.GetComponentsInChildren<Component>(true)
    3. Resources.FindObjectsOfTypeAll<Component>()
    1. Transform.Find() or Tranform.FindChild()
    Finds a child gameobject ,both active and inactive .

    transform.Find("InActiveGameObject_name")

    or

    transform.FindChild("InActiveGameObject_name")

    If name contains a ‘/’ character it will traverse the hierarchy like a path name.

    transform.Find("Child/InActive")

    1. Transform.GetComponentsInChildren()
    This also finds inactive gameobjects in child. If you want to include inactive child gameobjects then you must pass true as a parameter.

    transform.GetComponentsInChildren<Transform> (true)

    This will find all the gameobjects with Transform component in child.

    1. Resources.FindObjectsOfTypeAll()
    This will find all the gameobjects in the current scene , be it active or inactive.

    Resources.FindObjectsOfTypeAll<Transform>()

    Credits: www.unityrealm.com/how-to-find-inactive-gameobject-in-unity/
     
    Last edited: Sep 27, 2015
    RuanJacobz likes this.