Question by
Cryno1000 · Apr 01, 2020 at 10:00 PM ·
functionui imagefunction callweapon systemweaponchanging
Function starting next time it is called (Weapon swapping issue)
So I am making a game with the ability for players to swap weapons using the scroll wheel or by opening a weapon box (which gives the player a random weapon). When using the weapon box, it works fine but when swapping weapons, the UI element showing a picture of their weapon shows the wrong weapon. So basically it doesn't update until they swap weapons AGAIN, which by that point it would show their last weapon. (Code wall incoming)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class playercontroller : MonoBehaviour
{
public int player;
public Vector2 speed = new Vector2(50, 50);
public bool facingRight = true;
public Rigidbody2D rb2d;
public Sprite pistolimg;
public Sprite assaultrifleimg;
public Sprite shotgunimg;
public Sprite sniperimg;
public Sprite lmgimg;
public Sprite smgimg;
public Image weapon;
public int weaponselection;
private Vector2 movement;
private Rigidbody2D rigidbodyComponent;
public barrel barrel;
public depthscript depthscript;
public GameObject chest;
public Sprite openchest;
public float disttochest;
public bool canOpenChest;
private SpriteRenderer chestSpriteRenderer;
public GameObject weaponbox;
public Sprite openweaponbox;
public float disttoweaponbox;
public bool canOpenWeaponBox;
private SpriteRenderer weaponboxSpriteRenderer;
public chest chestscript;
public int money;
[HideInInspector] public bool weapon1;
[HideInInspector] public bool weapon2;
[HideInInspector] public bool weapon3;
[HideInInspector] public bool weapon2owned;
[HideInInspector] public bool weapon3owned;
[HideInInspector] public string weapon1name;
[HideInInspector] public string weapon2name;
[HideInInspector] public string weapon3name;
[HideInInspector] public string weapon1type;
[HideInInspector] public string weapon2type;
[HideInInspector] public string weapon3type;
[HideInInspector] public string weapon2prefix1;
[HideInInspector] public string weapon2prefix2;
[HideInInspector] public string weapon3prefix1;
[HideInInspector] public string weapon3prefix2;
[HideInInspector] public string currentweaponprefix1;
[HideInInspector] public string currentweaponprefix2;
public string currentweapontype;
[HideInInspector] public string currentweaponname;
public weaponbox weaponboxscript;
public Text currentweapon;
//controls
public string interact;
public string weaponswap;
public string weaponswapright;
public string weaponswapleft;
public string reload;
public string horizontalMove;
public string verticalMove;
public string horizontalLook;
public string verticalLook;
public GameObject crosshair;
public float crosshairdist;
private Vector2 look;
public bool keyboard;
public void OnLevelWasLoaded(int level)
{
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
if (sceneName != "charactercreation")
{
if (player == 1) this.gameObject.transform.position = new Vector3(-0.75f, 0.75f, 0f);
if (player == 2) this.gameObject.transform.position = new Vector3(0.75f, 0.75f, 0f);
if (player == 3) this.gameObject.transform.position = new Vector3(-0.75f, -0.75f, 0f);
if (player == 4) this.gameObject.transform.position = new Vector3(0.75f, -0.75f, 0f);
}
GetRefs();
WeaponNames();
WeaponSwapFunction();
WeaponSprites();
}
void Start()
{
DontDestroyOnLoad(this.gameObject);
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
if (sceneName != "charactercreation")
{
barrel.GetComponent<barrel>();
depthscript = GameObject.FindGameObjectWithTag("depth").GetComponent<depthscript>();
}
else if (sceneName == "charactercreation")
{
if (player == 1) this.gameObject.transform.position = new Vector3(-4, 2, 0f);
if (player == 2) this.gameObject.transform.position = new Vector3(4, 2, 0f);
if (player == 3) this.gameObject.transform.position = new Vector3(-4, -2, 0f);
if (player == 4) this.gameObject.transform.position = new Vector3(4, -2, 0f);
}
weapon1type = "Pistol";
weapon2type = "none";
weapon3type = "none";
weaponselection = 1;
rb2d = GetComponent<Rigidbody2D>();
weaponselection = 1;
}
void FixedUpdate()
{
if (rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();
// Move the game object
rigidbodyComponent.velocity = movement;
}
void Update()
{
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
if (sceneName != "charactercreation")
{
Aim();
MovementDef();
Openables();
currentweapon.text = currentweaponname;
if (keyboard == true)
{
if (Input.GetAxis(weaponswap) < 0f || Input.GetAxis(weaponswap) > 0f) WeaponSwapFunction();
}
else if (keyboard == false)
{
if (Input.GetButtonDown(weaponswapright) || Input.GetButtonDown(weaponswapleft)) WeaponSwapFunction();
}
if (keyboard == false)
{
float lookX = Input.GetAxis(horizontalLook);
float lookY = Input.GetAxis(verticalLook);
look = new Vector2(speed.x * lookX, speed.y * lookY);
}
if (Input.GetButtonDown(reload)) barrel.StartCoroutine("reloadcheck");
Vector2 cursorInWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
public void GetRefs()
{
if (player == 1)
{
currentweapon = GameObject.FindGameObjectWithTag("ui/currentweapon/currentweapon1").GetComponent<Text>();
weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon1").GetComponent<Image>();
}
else if (player == 2)
{
currentweapon = GameObject.FindGameObjectWithTag("ui/currentweapon/currentweapon2").GetComponent<Text>();
weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon2").GetComponent<Image>();
}
else if (player == 3)
{
currentweapon = GameObject.FindGameObjectWithTag("ui/currentweapon/currentweapon3").GetComponent<Text>();
weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon3").GetComponent<Image>();
}
else if (player == 4)
{
currentweapon = GameObject.FindGameObjectWithTag("ui/currentweapon/currentweapon4").GetComponent<Text>();
weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon4").GetComponent<Image>();
}
}
public void Openables()
{
chest = GameObject.FindGameObjectWithTag("items/chest");
chestSpriteRenderer = chest.GetComponent<SpriteRenderer>();
chestscript = chest.GetComponent<chest>();
weaponbox = GameObject.FindGameObjectWithTag("items/weaponbox");
weaponboxSpriteRenderer = weaponbox.GetComponent<SpriteRenderer>();
weaponboxscript = weaponbox.GetComponent<weaponbox>();
disttochest = Vector3.Distance(gameObject.transform.position, chest.transform.position);
disttoweaponbox = Vector3.Distance(gameObject.transform.position, weaponbox.transform.position);
if (canOpenChest == true && Input.GetButtonDown(interact) && chestscript.chestOpened == false)
{
chestSpriteRenderer.sprite = openchest;
chestscript.chestOpened = true;
money = money + chestscript.chestvalue;
}
if (canOpenWeaponBox == true && Input.GetButtonDown(interact))
{
weaponboxSpriteRenderer.sprite = openchest;
if (weapon2 == true && weapon3owned == true || weapon1 == true && weapon2type == "none")
{
weaponboxscript.randomweapon();
weapon2type = weaponboxscript.weapontype;
weapon2prefix1 = weaponboxscript.prefix1choice;
weapon2prefix2 = weaponboxscript.prefix2choice;
weaponselection = 2;
barrel.UpdateAmmo();
weapon2owned = true;
}
if (weapon3 == true || weapon2 == true && weapon3type == "none")
{
weaponboxscript.randomweapon();
weapon3type = weaponboxscript.weapontype;
weapon3prefix1 = weaponboxscript.prefix1choice;
weapon3prefix2 = weaponboxscript.prefix2choice;
weaponselection = 3;
barrel.UpdateAmmo();
weapon3owned = true;
}
WeaponNames();
WeaponSwapFunction();
WeaponSprites();
barrel.UpdateAmmo();
}
}
public void PrefixStuff()
{
if (barrel.currentPrefix1 == "Light " || barrel.currentPrefix2 == "Light ") speed = new Vector2(4, 4);
else speed = new Vector2(3, 3);
}
public void WeaponSwapFunction()
{
if (keyboard == true)
{
if (Input.GetAxis(weaponswap) < 0f)
{
weaponselection += 1;
CallWeaponSwapFunctions();
}
if (Input.GetAxis(weaponswap) > 0f)
{
weaponselection -= 1;
CallWeaponSwapFunctions();
}
}
else if (keyboard == false)
{
if (Input.GetButtonDown(weaponswapright)) weaponselection += 1;
else if (Input.GetButtonDown(weaponswapleft)) weaponselection -= 1;
}
if (weaponselection == 0) weaponselection = 3;
if (weaponselection == 1)
{
weapon1 = true;
weapon2 = false;
weapon3 = false;
currentweapontype = weapon1type;
currentweaponname = weapon1name;
currentweaponprefix1 = "";
currentweaponprefix2 = "";
}
if (weaponselection == 2)
{
weapon1 = false;
weapon2 = true;
weapon3 = false;
currentweapontype = weapon2type;
currentweaponname = weapon2name;
currentweaponprefix1 = weapon2prefix1;
currentweaponprefix2 = weapon2prefix2;
if (weapon2type == "none") weaponselection = 3;
}
if (weaponselection == 3)
{
weapon1 = false;
weapon2 = false;
weapon3 = true;
currentweapontype = weapon3type;
currentweaponname = weapon3name;
currentweaponprefix1 = weapon3prefix1;
currentweaponprefix2 = weapon3prefix2;
if (weapon3type == "none") weaponselection = 1;
}
}
public void WeaponNames()
{
weapon1name = "Pistol";
weapon2name = weapon2prefix1 + weapon2prefix2 + weapon2type;
weapon3name = weapon3prefix1 + weapon3prefix2 + weapon3type;
}
public void MovementDef()
{
float inputX = Input.GetAxis(horizontalMove);
float inputY = Input.GetAxis(verticalMove);
movement = new Vector2(speed.x * inputX, speed.y * inputY);
}
public void WeaponSprites()
{
if (currentweapontype == "Pistol") weapon.sprite = pistolimg;
if (currentweapontype == "Assault Rifle") weapon.sprite = assaultrifleimg;
if (currentweapontype == "Shotgun") weapon.sprite = shotgunimg;
if (currentweapontype == "Sniper") weapon.sprite = sniperimg;
if (currentweapontype == "LMG") weapon.sprite = lmgimg;
if (currentweapontype == "SMG") weapon.sprite = smgimg;
}
public void Aim()
{
crosshair.transform.localPosition = look * crosshairdist;
}
public void CallWeaponSwapFunctions()
{
//stuff in playercontroller (duh)
PrefixStuff();
WeaponNames();
//stuff in barrel
barrel.GetReloadSpeed();
barrel.MaxAmmos();
barrel.ResetPrefix();
barrel.GetPrefix();
barrel.Prefix1();
barrel.Prefix2();
WeaponSprites();
}
}
Could anyone show me what's wrong? Additionally, I'm having an issue where it doesn't even swap weapons sometimes.
Comment