Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Weapon Change Problem Code

Discussion in 'Scripting' started by UMURAS, Mar 17, 2020.

  1. UMURAS

    UMURAS

    Joined:
    Jun 15, 2019
    Posts:
    18
    How can I switch between the two guns with the space key, but now I can switch from gun 1 to gun 2, and I cannot switch from gun 2 to gun 1.

    Code here please help me

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

    public class SilahDegistirme : MonoBehaviour
    {
    public string[] silahEnv;
    public GameObject[] silahListe;
    public int suankiSilah;
    public bool Silah1, Silah2;
    void Start()
    {
    suankiSilah = 0;
    SilahDegistir(silahEnv[suankiSilah]);
    Debug.Log("AK47 Secildi");
    }


    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Space) && suankiSilah != 0 && silahEnv[0] != string.Empty)
    {
    SilahDegistir(silahEnv[0]);
    suankiSilah = 0;
    Debug.Log("AK47 Secildi");

    }

    if (Input.GetKeyDown(KeyCode.Space) && suankiSilah != 1 && silahEnv[1] != string.Empty)
    {
    SilahDegistir(silahEnv[1]);
    suankiSilah = 1;
    Debug.Log("Desert Eagle Secildi");

    }


    }

    public void SilahDegistir(string silahismi)
    {
    for(int i = 0; i < silahListe.Length; i++)
    {
    if(silahListe.name == silahismi)
    {
    silahListe.gameObject.SetActive(true);
    }
    else
    {
    silahListe.gameObject.SetActive(false);
    }
    }
    }
    }
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,078
    In your Update loop you're processing both 'if' statements one after each other. That means that it'll switch the first weapon and then switch it back again when you press space. For the second 'if' statement you should change it to 'else if' I think.
     
  3. UMURAS

    UMURAS

    Joined:
    Jun 15, 2019
    Posts:
    18
    Thank you very much. I asked you how could you understand this?
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,078
    Here's a simpler example:

    Code (CSharp):
    1. int var = 0;
    2.  
    3.  
    4. if( Input.GetKeyDown(KeyCode.Space) && var==0 )
    5. {
    6.    var = 1;
    7. }
    8.  
    9. if( Input.GetKeyDown(KeyCode.Space) && var==1 )
    10. {
    11.    var = 0;
    12. }
    If you run that and press the spacebar then this happens:

    1) First 'if' statement runs and sets var to 1
    2) Second 'if' statement runs and sets var back to 0

    If you have this instead:
    Code (CSharp):
    1. int var = 0;
    2.  
    3.  
    4. if( Input.GetKeyDown(KeyCode.Space) && var==0 )
    5. {
    6.    var = 1;
    7. }
    8. else if( Input.GetKeyDown(KeyCode.Space) && var==1 )
    9. {
    10.    var = 0;
    11. }
    Then this happens:

    1) First 'if' statement runs and sets var to 1
    2) Second 'if' statement doesn't run because the first 'if' statement ran

    If you press space again then:

    1) First 'if' statement doesn't run
    2) Second 'if' statement runs and sets var to 0
     
  5. UMURAS

    UMURAS

    Joined:
    Jun 15, 2019
    Posts:
    18
    Thank you so much for telling
     
    tonemcbride likes this.