Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Cryno1000 · Apr 03, 2020 at 08:53 AM · uiui imagecontrolsweaponweaponchanging

Function being delayed until the next time it is called?

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
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

286 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with reticle and image 0 Answers

Block Input.GetMouseButtonDown() when clicking on UI element 2 Answers

How to crop an alpha UI image? 0 Answers

Panel UI change color and revert to original color! 1 Answer

UI images with alpha cut out of them? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges