So i made a script which changes your weapons from Sword to Bazooka or vice-versa.
using UnityEngine;
using System.Collections;
public class Weapon_Switch : MonoBehaviour
{
private GameObject Sword;
private GameObject Cannon;
void Awake ()
{
Sword = GameObject.Find ("Sword");
}
void Update ()
{
WeaponSwitch ();
}
void WeaponSwitch()
{
if (Input.GetKey ("1"));
{
Sword.SetActive(true);
Debug.Log ("Zobens virsu");
}
if (Input.GetKey ("2"));
{
Sword.SetActive(false);
Debug.Log ("Zobens nost");
}
}
}
So why is it disabling and enabling my Sword in each frame? Anything to do with 1 or 2?
Edit 1 : I have set up 1 and 2 as key’s, i guess… ?
Because you’re terminating the if statement with a semi-colon. Curly braces are technically legal as a it only defines a block of code, no constraint and yip-ee, it gets invoked.
Try this:
using UnityEngine;
using System.Collections;
public class Weapon_Switch : MonoBehaviour
{
private GameObject Sword;
private GameObject Cannon;
void Awake ()
{
Sword = GameObject.Find ("Sword");
}
void Update ()
{
WeaponSwitch ();
}
void WeaponSwitch()
{
if (Input.GetKey (KeyCode.Alpha1))
{
Sword.SetActive(true);
Debug.Log ("Zobens virsu");
}
if (Input.GetKey (KeyCode.Alpha2))
{
Sword.SetActive(false);
Debug.Log ("Zobens nost");
}
}
}
Note: you can use the enum(KeyCode) to represent the key you would like, or mousebutton, joystick, etc.
KeyCode Enum Docs
GetKey Docs showing overloads for string and enum