Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by FritzPeace · Nov 04, 2015 at 12:21 PM · variablestringbooleanboolforeach

[Help] Getting variable based on string value.

Hello, so I am doing something which is becoming quite complex, and would like to know if someone knows how to do this, or what exactly I should do.

     public static bool[] boolHolder;
     public static bool yellow;
     public static bool flamingo;
     public static bool parrot;
     public static bool pelican;
     public static bool vulture;

I have the following bools. First I would like to know If I can put those static bools inside the array. Then what I want is:

     public static void MakeBoolTrue(string name)
     {
         //This is an idea of how it would work, however I am not sure if it would actually work.
         //For each bool I have inside my boolHolder, which would be: yellow, flamingo, parrot,
        //pelican and vulture, I check if this bool name is equal to the name string. 
        //If it is and the bool value is false, I make this bool true

         foreach (bool boolean in boolHolder)
         {
             if(boolean.name == name && boolean == false)
             {
                  boolean = true;
             }
         }
     }

Does anyone have any idea or different approach to make this logic. Baiscally, I am doing this to unlock a player and make it available to use. Then to know, I will probably just instead of making a void making a public static bool, and returning the bool value, by using a similar logic.

Thanks for the help! Fritz

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

2 Replies

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

Answer by Anathaerine · Nov 04, 2015 at 03:40 PM

You should probably use a Dictionary or a List that contains a custom class. But let's go with Dictionary in this case. Recommended viewing beforehand: https://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

so you could declare a:

 Dictionary<string, bool> boolHolder = new Dictionary<string, bool>();

in your code. Now that means you don't have to create a ton of other bool variables, you just have to initialize some dictionary entries like:

 boolHolder["yellow"] = false;
 boolHolder["flamingo"] = false;

and so on and so forth.

Then, your MakeBoolTrue function would simply look like:

 public void MakeBoolTrue(string name){
    boolHolder[name] = true;
 }

Just make sure that you add some checks in beforehand to make sure the name exists within the dictionary or else you'll get some exceptions.

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 FritzPeace · Nov 04, 2015 at 03:43 PM 0
Share

Thanks! What would be the checks?

avatar image FritzPeace · Nov 04, 2015 at 06:21 PM 0
Share

Okay I got a problem, and hope you can help :D @Anathaerine

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class newText : $$anonymous$$onoBehaviour {
 
     public static Dictionary<string, bool> playerHolder = new Dictionary<string, bool>();
 
     public void OnEnable()
     {
         if(playerHolder.Count == 0)
         {
             playerHolder.Add("yellow", true);
             playerHolder.Add("fla$$anonymous$$go", true);
             playerHolder.Add("parrot", true);
             playerHolder.Add("pelican", true);
             playerHolder.Add("vulture", true);
         }
         Load();
     }
     public void OnDisable()
     {
         Save();
     }
 
     public static void EnablePlayer(string name)
     {
         playerHolder[name] = true;
     }
 
     //Error is here...
     public static bool hasPlayer(string name)
     {
         return playerHolder[name];
     }
 
     public void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "test.dat");
         Data data = new Data();
         data.playerSave = playerHolder;
         bf.Serialize(file, data);
         file.Close();
     }
     public void Load()
     {
         if (File.Exists(Application.persistentDataPath + "test.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "test.dat", File$$anonymous$$ode.Open);
             Data data = (Data)bf.Deserialize(file);
             file.Close();
             playerHolder = data.playerSave;
         }
     }
 }
 
 [Serializable]
 class TestDataSave
 {
     public Dictionary<string, bool> playerSave;
 }

It gives me an error where I commented, saying Object Reference not set to an instance of an object.

avatar image
0

Answer by FritzPeace · Nov 04, 2015 at 07:38 PM

Okay I got a problem, and hope you can help :D @Anathaerine

using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;

public class newText : MonoBehaviour {

  public static Dictionary<string, bool> playerHolder = new Dictionary<string, bool>();
 
  public void OnEnable()
  {
      if(playerHolder.Count == 0)
      {
          playerHolder.Add("yellow", true);
          playerHolder.Add("flamingo", true);
          playerHolder.Add("parrot", true);
          playerHolder.Add("pelican", true);
          playerHolder.Add("vulture", true);
      }
      Load();
  }
  public void OnDisable()
  {
      Save();
  }
 
  public static void EnablePlayer(string name)
  {
      playerHolder[name] = true;
  }
 
  //Error is here...
  public static bool hasPlayer(string name)
  {
      return playerHolder[name];
  }
 
  public void Save()
  {
      BinaryFormatter bf = new BinaryFormatter();
      FileStream file = File.Create(Application.persistentDataPath + "test.dat");
      Data data = new Data();
      data.playerSave = playerHolder;
      bf.Serialize(file, data);
      file.Close();
  }
  public void Load()
  {
      if (File.Exists(Application.persistentDataPath + "test.dat"))
      {
          BinaryFormatter bf = new BinaryFormatter();
          FileStream file = File.Open(Application.persistentDataPath + "test.dat", FileMode.Open);
          Data data = (Data)bf.Deserialize(file);
          file.Close();
          playerHolder = data.playerSave;
      }
  }

}

[Serializable] class TestDataSave { public Dictionary playerSave; }

It gives me an error where I commented, saying Object Reference not set to an instance of an object.

Comment
Add comment · 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

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

32 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

Related Questions

checking a bool via string 0 Answers

Need help with writing a bool to call a function 0 Answers

how do i say this 1 Answer

Call a function When a bool changes value? 2 Answers

change variable from textAsset in dictionary 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