I need help with calling a string
public string LevelName;
boolBoy.slime2 = true;
i want to put the LevelName where the slime2 is.
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?
using UnityEngine; using System.Collections;
namespace $$anonymous$$ore$$anonymous$$ountains.CorgiEngine {
///
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);
}
}
}
}
i want to make it so when a player finishes a level it changes a bool in a script called singletion
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.
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
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.
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.
It will set the proper index in the slimeList to true.
It will save the slimeList to PlayerPrefs
The next level will be loaded
Upon loading the next level the slimeList will be loaded from PlayerPrefs
Your answer
Follow this Question
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