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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[HELP,C#] Update() Function not running at all!

Discussion in 'Scripting' started by Realskull, Nov 27, 2019.

  1. Realskull

    Realskull

    Joined:
    Nov 27, 2019
    Posts:
    9
    Hello Everyone!

    I have coded this to change weapons in my fps game. The weapon change, start() and SelectWeapon() functions work properly but the update() function doesn't work at all.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class WeaponControllerSwitching : MonoBehaviour
    4. {
    5.     public int selectedWeapon = 0;
    6.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         SelectWeapon();  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         int previousSelectedWeapon = selectedWeapon;
    17.         Debug.Log("Updating");
    18.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    19.         {
    20.             if (selectedWeapon >= transform.childCount - 1)
    21.                 selectedWeapon = 0;
    22.             else
    23.                 selectedWeapon++;
    24.  
    25.         }
    26.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    27.         {
    28.             if (selectedWeapon <= transform.childCount - 1)
    29.                 selectedWeapon = 0;
    30.             else
    31.                 selectedWeapon--;
    32.  
    33.         }
    34.  
    35.         if (previousSelectedWeapon != selectedWeapon)
    36.         {
    37.             SelectWeapon();
    38.         }
    39.     }
    40.  
    41.     void SelectWeapon()
    42.     {
    43.         int i = 0;
    44.         foreach (Transform weapon in transform)
    45.         {
    46.             if (i == selectedWeapon)
    47.                 weapon.gameObject.SetActive(true);
    48.             else
    49.                 weapon.gameObject.SetActive(false);
    50.             i++;
    51.         }
    52.     }
    53. }
    54.  
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,

    What do you mean with "doesn't work at all." Please elaborate and explain. Do you mean that your Update function does not run at all? Or do you mean that the code in your Update does not do what you imagine it should do? Or something else, like you get some errors you don't know how to deal with?
     
  3. Realskull

    Realskull

    Joined:
    Nov 27, 2019
    Posts:
    9
    Yes , sorry for not being clear.
    I believe the update() function is supposed to run every frame but it doesn't even run once!
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    If you put to your Update something like:
    Code (CSharp):
    1. Debug.Log("Update running.");
    Do you see anything in your console window? Always first check the basics if something seems to be failing.

    EDIT: just out of curiosity I checked your code, it does run just fine here, no errors when compiling. So there must be some logic error how you expect it to function. Or there's that unlikely situation that the Update really doesn't run. But try that Debug print first to see if it's running there.
     
  5. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    33
    I assume that the Update function is called, but doesn't result in anything happening.

    Reason for this is that you have 2 if-clauses that compare the same thing "if (Input.GetAxis("Mouse ScrollWheel") > 0f)". The second one probably needs to be "< 0f".

    What currently happens is that the first if-clause does selectedWeapon++ and then the second if-clause does selectedWeapon-- .. essentially putting you on the weapon you started with again. So no weapon is switched.

    EDIT: spelling + layout
     
    Last edited: Nov 27, 2019
  6. Remi_Tribia

    Remi_Tribia

    Joined:
    Apr 29, 2015
    Posts:
    40
    Open Visual Studio, set some breakpoints, click "Attach to Unity" and play the game in Unity. There you can see what's wrong line by line.
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    The OP might just have the object disabled or have not added script to any gameobject in active scene. Update will never run in both cases.
     
    Joe-Censored likes this.
  8. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @palex-nx yes of course. That's why I asked to test if the script produces any debug prints... It's a good place to start.
     
  9. Realskull

    Realskull

    Joined:
    Nov 27, 2019
    Posts:
    9
    Yes, the code compiles , no errors after compiling or while playing game.

    Yes , I put the Debug.Log("Updating") in the code and it doesn't output in the console!
     
  10. Realskull

    Realskull

    Joined:
    Nov 27, 2019
    Posts:
    9
    Yes but if the function is callimg amd running,then it should have outputted "Updating" string to the console which it doesn't.
     
  11. Realskull

    Realskull

    Joined:
    Nov 27, 2019
    Posts:
    9
    The weapons ( theres like 3 weapons object ) are children of a empty game object Melee. The melee is child of fps_hands object.

    I have attached the script to the Melee empty game object.
    The Melee empty game object are enabled always (i have checked it in inspecter)
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Almost certainly, the object is getting deactivated. Just just need to confirm that and find out why, and this code will help you do that:
    Code (csharp):
    1. void OnDisable() {
    2. Debug.Log("We got disabled!");
    3. }
    I'm fairly certain that at some point this object is getting disabled or destroyed, and this will help you find out where. If you see this line, click on it in the console, and look at the lines below it in the console. That's called the stacktrace, and it's basically the path the code took to get to that function. If you check out where it came from, you should be able to see why it got disabled.
     
  13. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Where DOES it output in the console? I trust you put in Debug.Log statements in Start(), etc to track the code flow?
     
    Joe-Censored likes this.
  14. Realskull

    Realskull

    Joined:
    Nov 27, 2019
    Posts:
    9
    Okay, I will test the code out.
     
  15. Realskull

    Realskull

    Joined:
    Nov 27, 2019
    Posts:
    9
    I put the debug.log inside the update() function. Codes in the start() work as expected.
     
  16. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please show your corrected code. It is Update() and not update()