Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Ashmit2020 · Feb 01, 2021 at 12:18 PM · script.unity 2denumhandling

calling enum State to change correctly but it is not changing

I did everything correctly but getting this error in my shop script: public class shop : MonoBehaviour {

   public TMPro.TextMeshProUGUI scoreText;
   public GameObject Item1;
   public GameObject Item2;
   public GameObject Item3;
   public GameObject Item4;
   public TMPro.TextMeshProUGUI notenough;
   public TMPro.TextMeshProUGUI notenough1;
   public TMPro.TextMeshProUGUI notenough2;
   public TMPro.TextMeshProUGUI notenough3;
   public GameObject buyButton;
   public GameObject buyButton1;
   public GameObject buyButton2;
   public GameObject buyButton3;
   public GameObject equipButton;
   public GameObject equipButton1;
   public GameObject equipButton2;
   public GameObject equipButton3;
   public float sec = 14f;
   public static bool isSold = false;
   public static bool isSold1 = false;
   public static bool isSold2 = false;
   public static bool isSold3 = false;
   /*public ButtonState state;
   public ButtonState1 state1;
   public ButtonState2 state2;
   public ButtonState3 state3;*/
   public LoadEveryThing someName;
 
   private Dictionary<GameObject, float> ItemPrices;
  
  
 void Start ()
 {
     scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
  
   ItemPrices = new Dictionary<GameObject, float>()
   {
    { Item1, 50f },
    { Item2, 80f },
    {Item3, 3500f},
    { Item4, 5000f },
    };
    notenough.enabled = false;
    notenough1.enabled = false;
    notenough2.enabled = false;
    notenough3.enabled = false;
    
 }
  
 public void PurchaseItem(GameObject Item)

{ foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {

          float price = item.Value;
          float val = PlayerPrefs.GetFloat ("Highscore"); 
          if(val >= price)
          {
          val -= item.Value;
           PlayerPrefs.SetFloat("Highscore", val);
           scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
               buyButton.SetActive(false);
               equipButton.SetActive(true);
               isSold = true;
               someName.state = ButtonState.Equip;
              // Take away the cost of the item from the player's currency
              //PlayerPrefs.GetFloat ("Highscore") -= item.Value;
          }
          else
          {
              Debug.Log("not enough money");
               notenough.enabled = true;
               notenough1.enabled = true;
               notenough2.enabled = true;
               notenough3.enabled = true;
              Invoke("noen", 2);
              isSold = false;
              someName.state = ButtonState.Buy;
          }
         }     
    }

}

 public void PurchaseItem1(GameObject Item)

{ foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {

          float price = item.Value;
          float val = PlayerPrefs.GetFloat ("Highscore"); 
          if(val >= price)
          {
          val -= item.Value;
           PlayerPrefs.SetFloat("Highscore", val);
           scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
               buyButton1.SetActive(false);
               equipButton1.SetActive(true);
               isSold1 = true;
               
              // Take away the cost of the item from the player's currency
              //PlayerPrefs.GetFloat ("Highscore") -= item.Value;
          }
          else
          {
              Debug.Log("not enough money");
               notenough.enabled = true;
               notenough1.enabled = true;
               notenough2.enabled = true;
               notenough3.enabled = true;
              Invoke("noen", 2);
              isSold1 = false;
          }
         }     
    }

}

 public void PurchaseItem2(GameObject Item)

{ foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {

          float price = item.Value;
          float val = PlayerPrefs.GetFloat ("Highscore"); 
          if(val >= price)
          {
          val -= item.Value;
           PlayerPrefs.SetFloat("Highscore", val);
           scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
               buyButton2.SetActive(false);
               equipButton2.SetActive(true);
               isSold2 = true;
              
              // Take away the cost of the item from the player's currency
              //PlayerPrefs.GetFloat ("Highscore") -= item.Value;
          }
          else
          {
              Debug.Log("not enough money");
               notenough.enabled = true;
               notenough1.enabled = true;
               notenough2.enabled = true;
               notenough3.enabled = true;
              Invoke("noen", 2);
              isSold2 = false;
          }
         }     
    }

}

 public void PurchaseItem3(GameObject Item)

{ foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {

          float price = item.Value;
          float val = PlayerPrefs.GetFloat ("Highscore"); 
          if(val >= price)
          {
          val -= item.Value;
           PlayerPrefs.SetFloat("Highscore", val);
           scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
            buyButton3.SetActive(false);
               equipButton3.SetActive(true);
               isSold3 = true;
               
              // Take away the cost of the item from the player's currency
              //PlayerPrefs.GetFloat ("Highscore") -= item.Value;
          }
          else
          {
              Debug.Log("not enough money");
               notenough.enabled = true;
               notenough1.enabled = true;
               notenough2.enabled = true;
               notenough3.enabled = true;
              Invoke("noen", 2);
              isSold3 = false;
          }
         }     
    }

}

/*public void buy() {

   if (val >= price)
   {
     PurchaseItem();
   }
   else
   {
     
   }*/
 

public void noen() { notenough.enabled = false; notenough1.enabled = false; notenough2.enabled = false; notenough3.enabled = false;

}

} this is the loadeverthing script:

public class LoadEveryThing : MonoBehaviour { // Types of buttons public GameObject BuyButton; / public GameObject BuyButton1; public GameObject BuyButton2; public GameObject BuyButton3;/ public GameObject EquipButton; /public GameObject EquipButton1; public GameObject EquipButton2; public GameObject EquipButton3;/

  public ButtonState state;
 
   void Start()
  {
      // What file to read save data from
      string path = Application.persistentDataPath + "\\buttonstate.save";
 
      // Get data and set state to that data
      if (File.Exists(path))
      {
          string data = File.ReadAllText(path);
          
          if (Enum.TryParse(data, out ButtonState State))
              state = State;
      }
 
      // Copy button properties onto this button
      // depending on the state
      switch (state)
      {
          case ButtonState.Buy:
              SetButton(BuyButton);
              break;
          case ButtonState.Equip:
              SetButton(EquipButton);
              break;


      }

       
      /*if(shop.isSold == true)
      {
          state = ButtonState.Equip;
          Debug.Log("working");
      }
      else if(shop.isSold == false)
      {
          state = ButtonState.Buy;
          Debug.Log(" buy working");
      }*/

  }

  void Update()
  {
  }
 
  void SetButton(GameObject button)
  {
      // Set on-click method of button
      Button myButton = GetComponent<Button>();
      Button targetButton = button.GetComponent<Button>();
 
      myButton.onClick = targetButton.onClick;
 
      // Set text of button
      TMPro.TextMeshProUGUI myText = GetComponentInChildren<TMPro.TextMeshProUGUI>();
      TMPro.TextMeshProUGUI targetText = button.GetComponentInChildren<TMPro.TextMeshProUGUI>();
 
      myText.text = targetText.text;
  }

   /*public static void SetEquipButton()
  {
   
  }

  public static void SetBuyButton()
  {
      
  }*/

}

saveeveything script:

using UnityEngine; using System.IO;

public class SaveEverything : MonoBehaviour { // What script to save public LoadEveryThing MyButton;

  public void Save()
  {
      // What data to save
      string saveData = MyButton.state.ToString();
     
 
      // Where the data is stored
      string path = Application.persistentDataPath + "\\buttonstate.save";
 
      
       // Writes data to file
       if (File.Exists(path))
       {
          File.WriteAllText(path, saveData/*, saveData1, saveData2, saveData3, 5*/);
       }
       else
       {
          File.Create(path).Close();
          File.WriteAllText(path, saveData/*, saveData1, saveData2, saveData3, 5*/);
       }
 }   

}

public enum ButtonState { Buy, Equip }

error I am getting:

NullReferenceException: Object reference not set to an instance of an object shop.PurchaseItem (UnityEngine.GameObject Item) (at Assets/scripts/shop.cs:78) UnityEngine.Events.InvokableCall1[T1].Invoke (T1 args0) (at :0) UnityEngine.Events.CachedInvokableCall`1[T].Invoke (System.Object[] args) (at :0) UnityEngine.Events.UnityEvent.Invoke () (at :0) UnityEngine.UI.Button.Press () (at C:/Users/anjaliP/Desktop/2020.1.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Users/anjaliP/Desktop/2020.1.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Users/anjaliP/Desktop/2020.1.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Users/anjaliP/Desktop/2020.1.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update() (at C:/Users/anjaliP/Desktop/2020.1.3f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:376)`

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by VeryAnnoyingCat · Jan 31, 2021 at 12:55 PM

When setting any variable from another script:-

  1. The variable needs to be public or protected. The former is easier.

  2. You need to store a reference the game object containing the script. The syntax for which is public "Script name" "Script variable name";

  3. In order to set the variable:- "Script variable name"."Name of the variable you want to set";

In your case, you would need to:-

Create a instance of Load Every Thing using public LoadEveryThing someName; in the script in which you are setting state, and set it in the inspector.
Then type someName.state = ButtonState.Equip;

Comment
Add comment · Show 2 · Share
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
avatar image Ashmit2020 · Feb 02, 2021 at 03:59 AM 0
Share

@VeryAnnoyingCat I think I did I did something wrong and my question changed, but after using your solution it works now. But the problem is the enum resets everytime I quit the game or change scene how can I stop the enum to reset.

avatar image VeryAnnoyingCat Ashmit2020 · Feb 07, 2021 at 10:53 AM 0
Share

I don't know what in the world you did since I was denied permission to view this for 4 days and as for your question, you need to shove the enum state into some static variable. Do some googling, without it you cannot be a programmer.

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

142 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

Related Questions

Need Help Push and pull script for co-op/multiplayer 0 Answers

How do I have a game object access the enums on another game object on collision? 1 Answer

can i get help with a minor debug please ? 2 Answers

Brackeys EnemyAi Script 0 Answers

ScriptableObject Drop-Down Menu with added variables? 1 Answer


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