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. Dismiss Notice

Resolved Why one if is working, but second not?

Discussion in 'Scripting' started by Mich53al, Sep 7, 2023.

  1. Mich53al

    Mich53al

    Joined:
    Jun 24, 2022
    Posts:
    4
    Hi, I have problem, that I can't solve. (My code can looks kinda... amateurishly)
    In my there is string in my script called MyType. If type is one, slot works different than slot with another type. The problem is only "Players inventory" type is working "Container" isnt. If I Debug.Log(myType) only "Players inventory shows". What is happening there? If you have question about my code, you feel free to ask.
    PS. Sorry for my bad English. My code below:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if(myType == "Players inventory")
    4.         {
    5.             if(FindObjectOfType<Inventory>().inventory.Length >= myNumber)
    6.             {
    7.                 GetComponent<Image>().sprite = FindObjectOfType<Inventory>().inventory[myNumber - 1].icon;  
    8.             }
    9.             else
    10.             {
    11.                 GetComponent<Image>().sprite = null;  
    12.             }
    13.         }
    14.  
    15.  
    16.         else if(myType == "Container")
    17.         {
    18.             Debug.Log("1");
    19.             //Jeżeli pojemnik jest otawrty
    20.             if(PlayerScript.containerOpen == true)
    21.             {
    22.                 if(myNumber <= PlayerScript.openedContainer.GetComponent<Container>().container.Length)
    23.                 {
    24.                         Debug.Log("2");
    25.                         if(PlayerScript.openedContainer.GetComponent<Container>().container[myNumber - 1] != null)
    26.                         {
    27.                             Debug.Log("3");
    28.                             GetComponent<Image>().sprite = PlayerScript.openedContainer.GetComponent<Container>().container[myNumber - 1].icon;  
    29.                         }
    30.                         else
    31.                         {
    32.                             GetComponent<Image>().sprite = null;        
    33.                         }
    34.                 }
    35.                 else
    36.                 {
    37.                     gameObject.SetActive(false);
    38.                 }
    39.            }
    40.            
    41.         }
    42.        
    43.     }
     
  2. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    The key to determining "flow" is logging as you are doing however logging in the Update method can get crazy because it logs so often.

    You need to see what sets "myType" and confirm that you actually set it and what value you set it to. The problem with setting things to a string constant (as you have) is there are lots of opportunities for typographical errors. For instance you set it to "container" but test for "Container". Try defining these values and reference the definitions instead. And don't name a variable myType ;)

    I'd recommend not logging "1" and "2" and "3" but rather something that identifies the condition so "Update.Container" for instance.

    Lots of GetComponent<Image().sprite sort of stuff going on as well. Ideally you should obtain a reference to the sprite and the Container outside of the Update method so you don't have to constantly search for it.
     
  3. Mich53al

    Mich53al

    Joined:
    Jun 24, 2022
    Posts:
    4
    myType is set in inspector. I checked this name thousands times.
    Yeah, I know I can make code easier but I want to fix that problem first
    Thank's for reply
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    You've posted this on the C# Job System sub-forum but your post has nothing to do with the Job System.

    The place for Generic Scripting is the Scripting sub-forum. I'll move the thread there for you.
     
    Bunny83 likes this.
  5. Mich53al

    Mich53al

    Joined:
    Jun 24, 2022
    Posts:
    4
    Okay, I will remember
     
    MelvMay likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Not necessary at all. Here is how YOU can answer questions about your own code!!

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.

    ALSO... this:

    Don't write crazy code like that.


    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    Putting it all one one line DOES NOT make it faster. That's not how compiled code works.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums


    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

    If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way(tm) success of accessing things in your game.
     
  7. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    You have an awful lot going on here, but work with this for starters:
    Code (CSharp):
    1. Inventory myInventory;
    2. Container myContainer;
    3. Image myImage;
    4.  
    5. void Update()
    6. {
    7.     if (myInventory == null)
    8.     {
    9.         print("Inventory was null, getting reference");
    10.         myInventory = FindObjectOfType<Inventory>().inventory;
    11.     }
    12.  
    13.     if (myContainer == null)
    14.     {
    15.         print("Container was null, getting reference");
    16.         myContainer = PlayerScript.openedContainer.GetComponent<Container>();
    17.     }
    18.  
    19.     if (myImage == null)
    20.     {
    21.         print("Image was null, getting reference");
    22.         myImage = GetComponent<Image>();
    23.     }
    24.  
    25.     if (myType == "Players inventory")
    26.     {
    27.         if(myInventory.Length >= myNumber)
    28.         {
    29.             myImage.sprite = myInventory[myNumber - 1].icon;
    30.         }
    31.         else
    32.         {
    33.             myImage.sprite = null;
    34.         }
    35.     }
    36.     else if (myType == "Container")
    37.     {
    38.         //Debug.Log("1");
    39.         //Jeżeli pojemnik jest otawrty
    40.         if(PlayerScript.containerOpen == true)
    41.         {
    42.             if(myNumber <= myContainer.container.Length)
    43.             {
    44.                 Debug.Log("2");
    45.                 if (myContainer.container[myNumber - 1] != null)
    46.                 {
    47.                     Debug.Log("3");
    48.                     myImage.sprite = myContainer.container[myNumber - 1].icon;
    49.                 }
    50.                 else
    51.                 {
    52.                     myImage.sprite = null;    
    53.                 }
    54.             }
    55.             else
    56.             {
    57.                 gameObject.SetActive(false);
    58.             }
    59.         }
    60.     }
    61. }
     
  8. Mich53al

    Mich53al

    Joined:
    Jun 24, 2022
    Posts:
    4
    I fixed it, thank you all for help! How to change post to solved? :>
     
    wideeyenow_unity likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Change the subject flair from "Question" to "Resolved" (or whatever it is)
     
    wideeyenow_unity likes this.