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

Question Changing behaviors when referencing another script?

Discussion in 'Scripting' started by ANickerson, Aug 16, 2021.

  1. ANickerson

    ANickerson

    Joined:
    Mar 3, 2021
    Posts:
    11
    I feel like I should know this I'm just drawing a blank.

    I have a MonoSingleton script for the items in my game and I'm referencing the item script from another script when the player wants to use the item... but I need it to not give the item if the player has none left

    Here is an example of the code I have:

    Code (CSharp):
    1.  public void UseGoggles()
    2.     {
    3.         if (Goggles <= 0)
    4.             Debug.LogWarning("None Left!");
    5.  
    6.         if (Goggles >= 1)
    7.             Goggles -= 1;
    8.             SaveSystem.SaveItems(this);
    9.     }
    This is from the item script

    Code (CSharp):
    1.  public void GiveGoggles()
    2.     {
    3.         goggles.SetActive(true);
    4.         Items.Instance.UseGoggles();
    5.     }
    This is the code referencing the item script... I know there is a simple way to have it not set them as active if the item count is 0... any help is greatly appreciated!
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,360
    Do you have method in your item script to get the item count?
     
  3. ANickerson

    ANickerson

    Joined:
    Mar 3, 2021
    Posts:
    11
    Yes I do... the item count is displayed above the toggles that the player can check to use the item before they start the level
     
  4. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    I am not sure if I understood your problem. Are you looking for something like this.?

    Items:
    Code (CSharp):
    1. public bool HasGoggles()
    2. {
    3.     return Goggles > 0;
    4. }
    Object:
    Code (CSharp):
    1. public void GiveGoggles()
    2. {
    3.     goggles.SetActive( Items.Instance.HasGoggles() );
    4.     Items.Instance.UseGoggles();
    5. }
    You could also return a bool from UseGoggles() directly and then use this in SetActive().
     
  5. ANickerson

    ANickerson

    Joined:
    Mar 3, 2021
    Posts:
    11
    ok this might work I'll have to give it a try!

    I should have been more clear I apologize.. the script referencing the items script is what is called from the "OnValueChange" in the inspector on the toggle... so when the player clicks the toggle it is supposed to give them the item, but the way I have it coded the item is given no matter what the number is.