I'm trying to set up an undo/redo system during runtime and couldn't get my code to work correctly. I have created an undo button, but when I moved my game object and press the undo button, the game object doesn't go back to its original states.
I just wanted to be able to have the end-user undo /redo his lastest action during runtime and not during Editor mode. I have made several attempts and have tried many different scripts but haven't made any progress. Any suggestions?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class undoit : MonoBehaviour
{
public class setting
{
public GameObject Obj;
public Vector3 Pos;
public Quaternion Rot;
public bool Deleted;
public void Restore()
{
Obj.transform.position = Pos;
Obj.transform.rotation = Rot;
Obj.SetActive(Deleted);
}
public setting(GameObject g)
{
Obj = g;
Pos = g.transform.position;
Rot = g.transform.rotation;
Deleted = g.activeSelf;
}
}
public List<setting> UndoList;
public void AddUndo(GameObject g)
{
setting s = new setting(g);
UndoList.Add(s);
}
public void Undo()
{
if (UndoList.Count > 0)
{
UndoList[UndoList.Count - 1].Restore();
UndoList.RemoveAt(UndoList.Count - 1);
}
}
void Start()
{
UndoList = new List<setting>();
}
}
First you need something you can store the relevant data to like e.g.
public struct ObjectState
{
// The transform this data belongs to
private Transform transform;
private Vector3 localPosition;
private Quaternion localRotation;
private Vector3 localScale;
private bool active;
public ObjectState (GameObject obj)
{
transform = obj.transform;
localPosition = transform.localPosition;
localRotation = transform.localRotation;
localScale = transform.localScale;
active = obj.activeSelf;
}
public void Apply()
{
transform.localPosition = localPosition;
transform.localRotation = localRotation;
transform.localScale = localScale;
transform.gameObject.SetActive(active);
}
}
Next I would use a wrapper class to store undo able changes like
public struct UndoableChange
{
private ObjectState _before;
private ObjectState _after;
public UndoableChange (ObjectState before, ObjectState after)
{
_before = before;
_after = after;
}
public void Undo()
{
_before.Apply();
}
public void Redo()
{
_after.Apply();
}
}
Or in case you need it for my objects at once you could use lists e.g. something like
public struct UndoableChange
{
private List<ObjectState> _before;
private List<ObjectState> _after;
public UndoableChange (List<ObjectState> before, List<ObjectState> after)
{
_before = before;
_after = after;
}
public void Undo()
{
foreach(var state in _before)
{
state.Apply();
}
}
public void Redo()
{
foreach(var state in _after)
{
state.Apply();
}
}
}
Then for the controller I would use two Stack. A Stack is "last in - first out" so exactly what you need. You add new entries using Push and by using Pop you retrieve the last added element and at the same time remove it from the stack. So it would look like
public static class UndoRedo
{
private static Stack<UndoableChange> undoStack = new Stack<UndoableChange>();
private static Stack<UndoableChange> redoStack = new Stack<UndoableChange>();
public static void Undo()
{
if(undoStack.Count == 0) return;
var lastAction = undoStack.Pop();
lastAction.Undo();
redoStack.Push(lastAction);
}
public static void Redo()
{
if(redoStack.Count == 0) return;
var lastAction = redoStack.Pop();
lastAction.Redo();
undoStack.Push(lastAction);
}
public static void AddAction(UndoableChange action)
{
redoStack.Clear();
undoStack.Push(action);
}
}
I dont know how exactly your user interacts with the objects but you now would always do something like
// at some point store the initial values
var before = new ObjectState (selectedGameObject);
//TODO Apply all your changes to the selected object
// get the new values
var after = new ObjectState (selectedGameObject);
// Log to the history
UndoRedo.AddAction(new UndoableChange(before, after));
or use lists for all selected objects accordingly e.g. simply using Linq
using System.Linq;
...
var before = selectedGameObjects.Select(obj => new ObjectState(obj)).ToList();
//TODO Apply all your changes to all the selected objects
var after = selectedGameObjects.Select(obj => new ObjectState(obj)).ToList();
UndoRedo.AddAction(new UndoableChange(before, after));
Note: Typed on smartphone but I hope the idea gets clear
Related
I want to create public Scene[] levels; and manually assign my levels to the array, then loop through it and generate level selection buttons, but it won't show up in the inspector.
Is there any workaround?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class True : MonoBehaviour
{
// Start is called before the first frame update
public static int money;
[SerializeField]
public SceneManager[] scenes;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void nextscencefirst()
{
SceneManager.LoadScene("Level2");
money++;
}
}
SceneManager is a built-in static class to control scenes during runtime. We can go to any scene by calling LoadScene method from that class. Also SceneManager cannot be seen in Inspector since it cannot be serialized class.
A reminder: Your scenes should be listed on build settings for these to work.
There are three ways to do what you want to do:
Method 1. Create a string list that holds names of scenes
public List<string> sceneNameList = new List<string>;
Method 2. Create a list of indices of scenes that added in build settings.
public List<int> sceneBuildIndexList = new List<int>
If the names of scenes are long or somehow difficult or you want to use sceneBuildIndex value it would be good to create a class and reach scenes from it
[System.Serializable]
public class SceneData
{
public int sceneBuildIndex;
public string sceneKey;
//public void LoadScene() //Maybe
}
public List<SceneData> sceneDataList = new List<SceneData>();
public SceneData GetSceneData(string key)
{
for (int i = 0; i < sceneDataList.Count; i++)
{
if (sceneDataList[i].sceneKey == key)
{
return sceneDataList[i];
}
}
return null;
}
Method 3: In Unity most classes are created from UnityEngine.Object, so it may let you assign scene from the Inspector.
[System.Serializable]
public class SceneData
{
public UnityEngine.Object scene;
public string key;
public void LoadScene()
{
if (scene == null)
{
Debug.LogError("Scene is not assigned");
return;
}
string pathToScene = AssetDatabase.GetAssetPath(scene);
SceneManager.LoadScene(pathToScene); //If the scene is not set in BuildSettings it will throw an error.
}
}
public List<SceneData> sceneDataList = new List<SceneData>();
public SceneData GetSceneData(string key)
{
for (int i = 0; i < sceneDataList.Count; i++)
{
if (sceneDataList[i].key == key)
{
return sceneDataList[i];
}
}
return null;
}
private void Awake()
{
SceneData data = GetSceneData("Level1");
if (data != null)
{
data.LoadScene();
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Cinemachine;
public class Waypoints : MonoBehaviour
{
[Header("Objects To Move")]
public Transform objectToMovePrefab;
public int numberOfObjectsToMove = 1;
[Header("Speed")]
public float speed;
public bool randomSpeed = false;
public float minRandomSpeed = 1;
public float maxRandomSpeed = 100;
private bool changeSpeedOnce = false;
private bool currentSpeedState;
[Header("Waypoints")]
[SerializeField] private List<Transform> waypoints;
[Header("Delay")]
public bool useDelay = false;
public float delay = 3;
public bool randomDelay = false;
public float minRandomDelay = 0.3f;
public float maxRandomDelay = 5;
[Header("LineRenderer")]
public LineRenderer lineRenderer;
public bool moveOnLineRenderer = false;
public List<Vector3> lineRendererPositions = new List<Vector3>();
[Header("Cinemachine Cameras")]
public CinemachineVirtualCamera virtualCamera;
private List<WaypointsFollower> waypointsFollowers = new List<WaypointsFollower>();
private void Start()
{
currentSpeedState = changeSpeedOnce;
for (int i = 0; i < numberOfObjectsToMove; i++)
{
var parent = GameObject.Find("Moving Object Parent");
var objectToMove = Instantiate(objectToMovePrefab, parent.transform);
objectToMove.name = "Platfrom";
waypointsFollowers.Add(objectToMove.GetComponent<WaypointsFollower>());
}
virtualCamera.Follow = waypointsFollowers[0].gameObject.transform;
virtualCamera.LookAt = waypointsFollowers[0].gameObject.transform;
foreach (Transform wp in waypoints)
{
lineRendererPositions.Add(wp.position);
}
if (moveOnLineRenderer)
{
foreach (WaypointsFollower wpf in waypointsFollowers)
{
wpf.go = true;
}
}
SpeedUpdater();
if (useDelay)
StartCoroutine(SendObjectstomoveWithDelay());
}
private void Update()
{
lineRendererPositions.Clear();
lineRendererPositions.AddRange(GetLinePointsInWorldSpace());
SpeedUpdater();
}
public int Count => waypoints.Count;
public Vector3 GetWaypoint(int index)
{
return waypoints[index].position;
}
public int CountLR => lineRendererPositions.Count;
public Vector3 GetLRWaypoint(int index)
{
return lineRendererPositions[index];
}
IEnumerator SendObjectstomoveWithDelay()
{
{
foreach (WaypointsFollower follower in waypointsFollowers)
{
if (randomDelay)
{
delay = Random.Range(minRandomDelay, maxRandomDelay);
}
yield return new WaitForSeconds(delay);
follower.go = true;
}
}
}
private void SpeedUpdater()
{
foreach (WaypointsFollower follower in waypointsFollowers)
{
if (randomSpeed)
{
follower.speed = Random.Range(minRandomSpeed, maxRandomSpeed);
}
else
{
follower.speed = speed;
}
}
}
Vector3[] GetLinePointsInWorldSpace()
{
var positions = new Vector3[lineRenderer.positionCount];
//Get the positions which are shown in the inspector
lineRenderer.GetPositions(positions);
//the points returned are in world space
return positions;
}
}
Inside the SpeedUpdtaer I'm using a flag and I want to be able to change the flag in run time too. The problem is calling the SpeedUpdater in the Update will make the foreach loop every frame and it might be expensive because in the List waypointsFollowers there might be a lot of items.
and about the delay part, I want to do that if I change the useDelay flag in run time then from that moment each moving object will get to the next waypoint and then from there the delay will start working. same if changing the useDelay flag to false at run time then don't wait at the next waypoint/s.
The delay is working fine but only in the Start for now and also the SpeedUpdater is working fine but I'm not sure if making foreach each frame is a good way. and not sure how to make and wherein the code that I will be able to change the speed and the speed random flag at run time without making the loop each frame.
I would use properties. With properties you can keep track in the variable value set, to conditionally or on value change execute some logic, like this (I did not compile check);
private bool useDelay = false;
public bool UseDelay { get => useDelay; set {
useDelay = value;
if (useDelay)
StartCoroutine(SendObjectstomoveWithDelay());
}};
private bool randomDelay = false;
public bool RandomDelay { get => randomDelay; set {
SpeedUpdater(value);
randomDelay = value;
}};
For that I would make also that SpeedUpdater() accepts the bool argument:
private void SpeedUpdater(bool randomSpeedArgument)
{
foreach (WaypointsFollower follower in waypointsFollowers)
{
if (randomSpeedArgument)
{
follower.speed = Random.Range(minRandomSpeed, maxRandomSpeed);
}
else
{
follower.speed = speed;
}
}
}
I was not very careful in adjusting to the logic of your question. The sample code is just to show you how you can run logic on value get/set. Other way are events, a bit more complicated option, and not much needed for this case I believe.
You can give a read to the docs
quick introduction to my project. I am trying to make a game in unity game engine and I am working on its shop system. Everything about the shop system is complete but one problem is still there. I need to save the State of button that is (MyButton) in my game scene and change it with (BuyButton) or (EquipButton) the saving part works but setting the button doesn't, I am using enum and switch statements for that part. These are the scripts of those two things.
script with the enum:
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
}
At the last part of script the enum is declared, with Buy and Equip inside it and rest of the script is about saving MyButton in a string.
Script with the switch statement:
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System;
using TMPro;
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()
{
}*/
}
In the script the switch statement works like this if the enum ButtonState is ButtonState.Buy then it actives buy button else it active and sets Equip button. But the problem is I don't know how to change the ButtonScript State through script. So the switch statement doesn't have anything to change and save BuyButton to EquipButton. I had already tried this /*I made this variable of type ButtonState*/ public ButtonState state and then I did this to change the enum State state = ButtonState.Equip; but it is not changing the State of the enum. I can see that in the inspector and there are not errors as well. So what should I do?
This is the script where I am calling to change the enum state:
the script is too long so this is the method where I am calling it and I also as told I already made a variable of type public ButtonState state;
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;
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<GameObject, float> 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;
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);
state = ButtonState.Buy;
}
}
}
}
public void noen()
{
notenough.enabled = false;
notenough1.enabled = false;
notenough2.enabled = false;
notenough3.enabled = false;
}
}
If any thing is unclear let me know.
Thanks,
I am looking to do a specific task for a small unity game I am doing for class.
In it, I am trying to hash a class that contains variables and a method that is specific to each class created for the dictionary. The variables work fine as they do not need to be static and are not abstract, the method however I am struggling to work with.
Here is my entire script being used.
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
public class EnemyZ : MonoBehaviour
{
// Class used to hash names health assets and then move options
public class EnemySet
{
public string EnemyName { get; set; }
public float EnemyHealth { get; set; }
public void Moves()
{
}
}
//setting EnemySet with names health (assets and move options)
public static void Setup()
{
var cards = new Dictionary<string, EnemySet>()
{
{ "Slime", new EnemySet { EnemyName="Slime", EnemyHealth= 25} },
{ "Flaz", new EnemySet { EnemyName="Flaz", EnemyHealth= 34} },
{ "BandShee", new EnemySet { EnemyName="BandShee", EnemyHealth= 45} },
{"Fan-Natic", new EnemySet{EnemyName = "Fan-Natic", EnemyHealth = 20} }
};
}
void Update()
{
}
}
I am looking to have the Move function be overridden and callable.
How would I set this dictionary of class/method's up and how would I call it once it has been correctly added to the dictionary?
I think you are approaching your class structure and the approach to overwriting your abstract methods incorrectly which is causing the confusion around the dictionary.
I would create an interface for enemies that defines what your enemies need to contain and perform. You can then create an abstract base class the implements common functionality for enemies. Each enemy type should then inherit from your base class.
See below as an example:
// all enemy types must implement the following
public interface IEnemy {
string EnemyName { get; }
float EnemyHealth { get; }
void Move ();
}
// abstract base class for common functionality
public abstract class Enemy : IEnemy {
protected float speed = 0.1f;
public string EnemyName { get; protected set; }
public float EnemyHealth { get; protected set; }
public virtual void Move () {
Debug.Log ($"{EnemyName} moving at {speed}");
}
}
public class Slime : Enemy {
public Slime () {
speed = 0.1f;
EnemyName = "Slimer";
EnemyHealth = 100f;
}
public override void Move () {
Debug.Log ($"{EnemyName} moving at {speed}");
}
}
public class Flaz : Enemy {
public Flaz () {
speed = 0.5f;
EnemyName = "Flaz";
EnemyHealth = 50f;
}
public override void Move () {
Debug.Log ($"{EnemyName} moving at {speed}");
}
}
public class Test : MonoBehaviour {
readonly List<IEnemy> Enemies = new List<IEnemy> ();
void Start () {
var slimer = new Slime ();
Debug.Log ($"{slimer.EnemyName} initialized with health of {slimer.EnemyHealth}");
slimer.Move ();
var flaz = new Flaz ();
Debug.Log ($"{flaz.EnemyName} initialized with health of {flaz.EnemyHealth}");
flaz.Move ();
Enemies.Add (slimer);
Enemies.Add (flaz);
Debug.Log ($"Added {Enemies.Count} enemies");
}
}
I'd say that having a better understanding of classes and interfaces would be good if you wanted to store your enemies in code (the easy way if you're good at object oriented code).
However to answer the question directly I'm going to assume that you're loading enemies from a database or a file or some other non-code format.
In that case I'd go with something like
// Class used to hash names health assets and then move options
public class EnemySet
{
public string EnemyName { get; set; }
public float EnemyHealth { get; set; }
public Action Moves { get; set; }
}
Then when ever you are reading from your datasource to set up the dictionary of enemies I'd have something like
public class EnemyLoader
{
public void MovePawn() => console.WriteLine("I'm a chess piece");
public void MoveZombie() => console.WriteLine("Brains");
public void MoveSlime() => console.WriteLine("Wobbo");
public Action PickMovement(string moveType)
{
switch(moveType)
{
case "pawn": return MovePawn;
case "pawn": return MoveZombie;
default: return MoveSlime;
}
}
public Dictionary<string, EnemySet> LoadEnemies()
{
var dataSource = ReadDataFromSomewhere();
return dataSource.ToDictionary(
k => k.EnemyName,
v => new EnemySet
{
EnemyName = v.EnemyName,
EnemySpeed = v.EnemySpeed,
Move = PickMovement(v.MoveType)
});
}
}
I have a Quiz game with multiple choices. I Want to show random question however sometimes the same Questions is being picked more than once. I want to remove the selected questions out of my list so it doesn't get picked anymore..
This is the Game Manager that shows my question's
** Check the ShowQuestion Function there where i do my random. i basically want when a question is picked to not show again
public class GameController : MonoBehaviour {
private dataController DataController;
private roundData currentRoundData;
// QUestions that we will be working with in this round
private questionData[] questionPool;
private List<GameObject> answerButtonGameobjects = new List<GameObject>();
private List<GameObject> QuestionGameobjects = new List<GameObject>();
[Header("OTHER")]
public SimpleObjectPool answerButtonObjectPool;
public Transform answerButtonParent;
[Tooltip("Variables")]
// is the game going?
private bool isRoundActive;
private float timerRemaing;
// What number question we are on
private int questionIndex;
private int questionNumber = 1;
private int totalQuestions;
[Header("UI")]
[Header("TEXT ")]
public Text questionText;
public Text scoreDisplayText;
public Text timeRemainingDisplayText;
public Text highScoreText;
public Text questionIndexText;
void Start () {
DataController = FindObjectOfType<dataController>();
currentRoundData = DataController.getCurrentRoundData();
// stores our questions
questionPool = currentRoundData.questions;
timerRemaing = currentRoundData.timeLimitInSeconds;
questionIndex = 0;
totalQuestions = 10;
ShowQuestion();
}
// Show our 1st question
private void ShowQuestion()
{
RemoveAnsweredButtons();
// Hold current question data from our pool
questionData QuestionData = questionPool[Random.Range(0, totalQuestions)];
questionText.text = QuestionData.questionText;
for (int i = 0; i < QuestionData.answers.Length; i++)
{
GameObject answerButtonGameobject = answerButtonObjectPool.GetObject();
answerButtonGameobject.transform.SetParent(answerButtonParent);
answerButtonGameobjects.Add(answerButtonGameobject);
AnswerButton answerButton = answerButtonGameobject.GetComponent<AnswerButton>();
answerButton.Setup(QuestionData.answers[i]);
}
}
/// <summary>
/// Removes the old answers button before we display new ones.
/// </summary>
private void RemoveAnsweredButtons()
{
while (answerButtonGameobjects.Count > 0)
{
answerButtonObjectPool.ReturnObject(answerButtonGameobjects[0]);
answerButtonGameobjects.RemoveAt(0);
}
}
private void UpdateQuestionIndex()
{
questionIndex++;
questionNumber++;
questionIndexText.text = "Question " + (questionNumber.ToString()) + " out of 10";
}
}
The Questions and Answers are being generated using a Simple pool system that was taken from Unity Site.
Here's the pooling class
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling class
public class SimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances of
public GameObject prefab;
// collection of currently inactive instances of the prefab
private Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefab
public GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return that
if (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instance
else
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instance
return spawnedGameObject;
}
// Return an instance of the prefab to the pool
public void ReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the pool
if(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy it
else
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came from
public class PooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
It's untested, and vanilla 'psuedo' C# to give you a guideline
private List<int> _questions = null;
private int _current = 0;
void Start()
{
_questions = Enumerable.Range(0, questionPool.Length).ToList();
Shuffle(_questions);
}
void Shuffle(List<int> list)
{
// put fisher-yates algorithm here to shuffle the numbers
}
void ShowQuestion()
{
var idx = _questions[_current++];
var questionData = questionPool[idx];
}