So I am trying to create a simple gun switch script, following a tutorial. I’m starting out with only the glock and AK for now, but I will add more once I get the first 2 done. (I am very new to coding.) Whenever I try anything regarding it, I just create more errors. I am seeking help, if anyone can!
Here is the video:
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weaponSwitch : MonoBehaviour {
public GameObject Glock18;
public GameObject AK47;
public GameObject M4Carbine;
public GameObject L96;
public GameObject Shotgun;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.E)) {
RaycastHit hit;
if (Physics.Raycast (Camera.main.transform.forward, transform.forward, out hit));
if (hit.transform.tag == "item")
if(hit.transform.gameObject.name.Contains("World Model Glock-18")){
if(weaponOut==1){
changeWeapon(1);
if (weaponOut == 1) {
Destroy (hit.transform.gameObject);
Instantiate (Glock18, transform.root.transform.position + new Vector3 (0, 2, 0), Quaternion.identity);
}else if(weaponOut==2){
Destroy(hit.transform.gameObject);
Instantiate(AK47, transform.root.transform.position+new Vector3(0, 2, 0), Quaternion.identity);
}
}
}
}
}
}
}
void changeWeapon(int weapon){
if (weaponIn == 1) {
weaponOut == 1;
transform.FindChild ("World Model Glock-18").gameObject.SetActive (false);
transform.FindChild ("World Model AK-47").gameObject.SetActive (true);
} else if (weaponIn == 2)
weaponOut == 2;
transform.FindChild ("World Model Glock-18").gameObject.SetActive (true);
transform.FindChild ("World Model AK-47").gameObject.SetActive (false);
}