Void can't be used in this context?

Alright, so I am trying to make my own Weapon Manager, adapting from other people’s code. C# won’t let me put a void inside of the update. I get that it won’t let me. What I need help with is finding a solution. I want to use OnTriggerEnter and a way to pick up more than one weapon. My other problem is that I have two firearms that I have in my scene. I am currently able to pick up both at the same time, and they overlap. I don’t want them to overlap. Only if C# would let me put a void inside of Update…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponManager : MonoBehaviour {

    public GameObject F2000;
    public GameObject USP;
    public Collider _collider;
    int weaponOut=1; //1= F2000 2=USP
    public GameObject Player;


    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update ()
    {

        if (Input.GetKeyDown (KeyCode.E))
        {

            Debug.Log ("Pressed E");

            void OnTriggerEnter (Collider _collider)
            {
                if (_collider.gameObject.tag == "Player")
                {
                    if(_collider.gameObject.name.Contains("USP45"))
                    {
                            if (weaponOut == 1)
                            {
                                Destroy(gameObject.transform);
                                Instantiate(USP,transform.root.transform.position+new Vector3(0,2,0)+ transform.forward*2,Quaternion.identity);
                            }
                            else if(weaponOut == 2)
                            {
                                Destroy(gameObject.transform);
                                Instantiate(F2000,transform.root.transform.position+new Vector3(0,2,0)+ transform.forward*2,Quaternion.identity);
                            }

                    }
                    changeWeapon(1);

                    if(_collider.gameObject.name.Contains("FN F-2000"))
                    {
                        if (weaponOut == 1)
                        {
                            Destroy(gameObject.transform);
                            Instantiate(USP,transform.root.transform.position+new Vector3(0,2,0)+ transform.forward*2,Quaternion.identity);
                        }
                        else if(weaponOut == 2)
                        {
                            Destroy(gameObject.transform);
                            Instantiate(F2000,transform.root.transform.position+new Vector3(0,2,0)+ transform.forward*2,Quaternion.identity);
                        }

                    }
                    changeWeapon(2);
                }
            }
        }
    }

    void changeWeapon(int weapon)
    {
        if (weapon == 1) {
            weaponOut = 1;
            transform.FindChild ("FN F-2000").gameObject.SetActive (false);
            transform.FindChild ("USP45").gameObject.SetActive (true);
        } else if (weapon == 2) {
            weaponOut = 2;
            transform.FindChild ("FN F-2000").gameObject.SetActive (true);
            transform.FindChild ("USP45").gameObject.SetActive (false);

        }
    }
}

You’re trying to put the OnTriggerEnter() method inside another method, which isn’t how it works. Put it on its own elsewhere in the class. You don’t nest functions in C# :slight_smile:

1 Like