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 Obarstar · Aug 11, 2016 at 12:26 AM · stringboolean

I need help with calling a string

 public string LevelName;

     boolBoy.slime2 = true;

i want to put the LevelName where the slime2 is.

Comment
Add comment · Show 5
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 TBruce · Aug 11, 2016 at 01:10 AM 0
Share

You are trying to fit a square peg into a round hole.

LevelName is a string and boolBoy.slime2 is a bool.

What you are asking can not be done so you need to explain what you are trying to do a little better.

boolBoy is a class. What does boolBoy look like?

avatar image Obarstar TBruce · Aug 11, 2016 at 01:19 AM 0
Share

using UnityEngine; using System.Collections;

namespace $$anonymous$$ore$$anonymous$$ountains.CorgiEngine {
///

/// Add this class to a trigger and it will send your player to the next level /// public class FinishLevel : ButtonActivated { public string LevelName; public singleton boolBoy; public string bloop; /// /// When the button is pressed we start the dialogue /// public override void TriggerButtonAction() { GoToNextLevel(); }

     public virtual void GoToNextLevel()
     {
         if (Level$$anonymous$$anager.Instance!=null)
         {
             Level$$anonymous$$anager.Instance.GotoLevel(LevelName);

             // Finds the object the script "IGotBools" is attached to and assigns it to the gameobject called g.
             GameObject g = GameObject.FindGameObjectWithTag("singleton");
             //assigns the script component "IGotBools" to the public variable of type "IGotBools" names boolBoy.
             boolBoy = g.GetComponent<singleton>();

             // accesses the bool named "isOnFire" and changed it's value.
             boolBoy.slime1 = true;
             Debug.Log("Hello", gameObject);

         }
         else
         {
             LoadingScene$$anonymous$$anager.LoadScene(LevelName);
         }
     }
 }

}

avatar image Obarstar TBruce · Aug 11, 2016 at 01:20 AM 0
Share

i want to make it so when a player finishes a level it changes a bool in a script called singletion

avatar image TBruce Obarstar · Aug 11, 2016 at 01:37 AM 0
Share

You need to show what the singleton class looks like.

BTW, singleton is not a good choice for a class name. You could better name it Level$$anonymous$$anagerSingleton, Game$$anonymous$$anagerSingleton or even SimpleSingleton. But just singleton is confusing and not very descriptive.

Show more comments

1 Reply

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

Answer by TBruce · Aug 11, 2016 at 10:03 PM

First let me say that I think you are over complicating this.

From what I am gathering here it looks like when you are ready to go to the next level you want to set a bool value that the current level (denoted by slime - slime13) to true and then load the next level.

But you will also want to be able save and load this information to PlayerPrefs so that the information can be retained from one level to another.

First it is more efficient to use an array of bool values instead of 13 bool values (this also makes it easier to expand the number of values needed) like this

 public bool[] slimeList;

Take a look at the following class

 using UnityEngine;
 using System.Collections;
 using System;
 using UnityEngine.SceneManagement;
 
 public class SlimeMananger : MonoBehaviour
 {
     //public string weep;
     
     // public int LEVEL;
 
     public bool[] slimeList;
     
     public int minimumArrayLength = 13;
 
     static private int endianDiff1;
     static private int endianDiff2;
     static private int idx;
     static private byte [] byteBlock;
 
     enum ArrayType {atFloat, atInt32, atBool}
 
     // Use this for initialization
     void Start()
     {
         // make sure that the list is at least as large as the number of available scenes
         int listLength = ((slimeList.Length > 0) ? slimeList.Length : minimumArrayLength);
         if (SceneManager.sceneCountInBuildSettings > listLength)
         {
             listLength = SceneManager.sceneCountInBuildSettings;
         }
         slimeList = GetBoolArray("SlimeList", false, listLength);
     }
     
     // go to the next available level
     // note: all levels must be added to "Scenes in Build" by selecting File\Build Settings in the main menu
     public void GoToNextLevel()
     {
         int sceneIndex = SceneManager.GetActiveScene().buildIndex;
 
         slimeList[sceneIndex] = true;         // set the current index to true
         SetBoolArray("SlimeList", slimeList); // save the slimeList to PlayerPrefs
         if (sceneIndex < (SceneManager.sceneCountInBuildSettings - 1))
         {
             SceneManager.LoadScene(sceneIndex + 1);
         }
     }
 
     // save the slimeList when the application closes
     private void OnApplicationQuit()
     {
         SetBoolArray ("SlimeList", slimeList);
     }
 
     // saves a bool array to PlayerPrefs
     public static bool SetBoolArray (String key, bool[] boolArray)
     {
         // Make a byte array that's a multiple of 8 in length, plus 5 bytes to store the number of entries as an int32 (+ identifier)
         // We have to store the number of entries, since the boolArray length might not be a multiple of 8, so there could be some padded zeroes
         var bytes = new byte[(boolArray.Length + 7)/8 + 5];
         bytes[0] = System.Convert.ToByte (ArrayType.atBool);    // Identifier
         var bits = new BitArray(boolArray);
         bits.CopyTo (bytes, 5);
         Initialize();
         ConvertInt32ToBytes (boolArray.Length, bytes); // The number of entries in the boolArray goes in the first 4 bytes
 
         return SaveBytes (key, bytes);    
     }
 
     // gets a bool array from PlayerPrefs
     public static bool[] GetBoolArray (String key, int defaultSize, bool defaultValue)
     {
         if (PlayerPrefs.HasKey(key))
         {
             var bytes = System.Convert.FromBase64String (PlayerPrefs.GetString(key));
 
             if (bytes.Length < 5)
             {
                 Debug.LogError ("Corrupt preference file for " + key);
                 return new bool[0];
             }
             if ((ArrayType)bytes[0] != ArrayType.atBool)
             {
                 Debug.LogError (key + " is not a boolean array");
                 return new bool[0];
             }
             Initialize();
 
             // Make a new bytes array that doesn't include the number of entries + identifier (first 5 bytes) and turn that into a BitArray
             var bytes2 = new byte[bytes.Length-5];
             System.Array.Copy(bytes, 5, bytes2, 0, bytes2.Length);
 
             var bits = new BitArray(bytes2);
             // Get the number of entries from the first 4 bytes after the identifier and resize the BitArray to that length, then convert it to a boolean array
             bits.Length = ConvertBytesToInt32 (bytes);
             var boolArray = new bool[bits.Count];
             bits.CopyTo (boolArray, 0);
 
             // make sure that the array is at least as large as the defaultSize
             if (boolArray.Length < defaultSize)
             {
                 var tempBoolArray = new bool[defaultSize];
                 for(int i = 0; i < defaultSize; i++)
                 {
                     if (i < boolArray.Length)
                     {
                         tempBoolArray[i] = boolArray[i];
                     }
                     else
                     {
                         tempBoolArray[i] = defaultValue;
                     }
                 }
                 return tempBoolArray;
             }
             else
             {
                 return boolArray;
             }
         }
         return new bool[0];
     }
 
     // gets a bool array from PlayerPrefs
     public static bool[] GetBoolArray (String key, bool defaultValue, int defaultSize) 
     {
         if (PlayerPrefs.HasKey(key))
         {
             return GetBoolArray(key, defaultSize, defaultValue);
         }
 
         var boolArray = new bool[defaultSize];
         for(int i=0;i<defaultSize;i++)
         {
             boolArray[i] = defaultValue;
         }
         return boolArray;
     }
 
     private static bool SaveBytes (String key, byte[] bytes)
     {
         try
         {
             PlayerPrefs.SetString (key, System.Convert.ToBase64String (bytes));
         }
         catch
         {
             return false;
         }
         return true;
     }
 
     private static int ConvertBytesToInt32 (byte[] bytes)
     {
         ConvertFrom4Bytes (bytes);
         return System.BitConverter.ToInt32 (byteBlock, 0);
     }
 
     private static void ConvertInt32ToBytes (int i, byte[] bytes)
     {
         byteBlock = System.BitConverter.GetBytes (i);
         ConvertTo4Bytes (bytes);
     }
  
     private static void ConvertTo4Bytes (byte[] bytes)
     {
         bytes[idx  ] = byteBlock[    endianDiff1];
         bytes[idx+1] = byteBlock[1 + endianDiff2];
         bytes[idx+2] = byteBlock[2 - endianDiff2];
         bytes[idx+3] = byteBlock[3 - endianDiff1];
         idx += 4;
     }
 
     private static void ConvertFrom4Bytes (byte[] bytes)
     {
         byteBlock[    endianDiff1] = bytes[idx  ];
         byteBlock[1 + endianDiff2] = bytes[idx+1];
         byteBlock[2 - endianDiff2] = bytes[idx+2];
         byteBlock[3 - endianDiff1] = bytes[idx+3];
         idx += 4;
     }
 
     private static void Initialize ()
     {
         if (System.BitConverter.IsLittleEndian)
         {
             endianDiff1 = 0;
             endianDiff2 = 0;
         }
         else
         {
             endianDiff1 = 3;
             endianDiff2 = 1;
         }
         if (byteBlock == null)
         {
             byteBlock = new byte[4];
         }
         idx = 1;
     }
 }

You have two public variables

 public bool[] slimeList;
 public int minimumArrayLength = 13;

And the way that this class is set up slimeList could just as well be a private be cause at runtime it either loads the current list of values from PlayerPrefs or it creates the list of values (all values not loaded from PlayerPrefs are initialized to false). All of this ensures that the list is of the proper size

  1. The length must be equal to or greater than the number of scenes in the "Scenes in Build" list - you can find this by selecting File\Build Settings in the main menu.

  2. The length must be equal to or greater than the minimumArrayLength public variable set in the inspector.

To use this first create an empty GameObject, call it something meaningful like SlimeManager and attach this class. Now make a prefab out of this newly created object as you will need to add it to every scene.

Loading and saving of values will now automatically happen on game start, changing levels or application quit.

Next in FinishLevel.cs do this (you also need to add this to every scene, except the last one)

 using UnityEngine;
 using System.Collections;
 
 namespace MoreMountains.CorgiEngine
 {
     ///
     /// Add this class to a trigger and it will send your player to the next level ///
     public class FinishLevel : ButtonActivated
     {
         public SlimeMananger slimeMananger;
 
         public void Start()
         { 
             // precaution
             if (slimeMananger == null)
             {
                 Debug.LogError("slimeMananger not set in the inspector. Please set and try again.");
                 
                 // attempting to find SlimeMananger
                 if ((GameObject.Find("SlimeMananger") != null) && (GameObject.Find("SlimeMananger").GetComponent<SlimeMananger>() != null))
                 {
                     slimeMananger = GameObject.Find("SlimeMananger").GetComponent<SlimeMananger>();
                 }
             }
         }
 
         /// /// When the button is pressed we start the dialogue ///
         public override void TriggerButtonAction()
         {
             if (slimeMananger != null)
             {
                 slimeMananger.GoToNextLevel();
             }
         }
     }
 }

Now TriggerButtonAction() will function as you like.

  1. It will set the proper index in the slimeList to true.

  2. It will save the slimeList to PlayerPrefs

  3. The next level will be loaded

  4. Upon loading the next level the slimeList will be loaded from PlayerPrefs

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

64 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

Related Questions

[Help] Getting variable based on string value. 2 Answers

checking a bool via string 0 Answers

Use String as a bool 0 Answers

Check if a string is empty and fill it with something? 3 Answers

Boolean not functioning as intended. 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