How to create an array of clones for a seperate Save/Load script
There are answers to similar questions out there, but as a total novice I can't quite put them together.
I want all clones created from a prefab to be put into an array so that said array can be added to another script for the purpose of saving and loading the scene. At the moment, I am unsure how to check if I am adding objects to the array and how the array can be accessed from my SaveLoad script.
#pragma strict
var memory : Rigidbody;
var throwPower : float;
var memoryCounter = 0;
public var memories = new Array ();
//instantiates memory clone on click, renames each clone with identifier and adds object to an Array
function OnClick () {
var clone : Rigidbody;
clone = Instantiate(memory, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.forward * throwPower);
clone.name = "clone" + memoryCounter.ToString();
memoryCounter++;
memories.push (clone);
}
I have managed to get my array to work as I hoped, although I am sure I have not used the most efficient method. I just added a tag to the prefab that gets instantiated and used FindGameObjectsWithTag() to put them into a new array (replacing the old one) each time a new one is created. I will update here when I see if it is working as expected in my SaveLoad script.
#pragma strict
var memory : Rigidbody;
var throwPower : float;
var memoryCounter = 0;
public var memories = new Array ();
//finds all memory objects with tag and replaces memories array with new version of array
function Update ()
{
var memoryCheck = GameObject.FindGameObjectsWithTag("$$anonymous$$emory");
memories = new Array(memoryCheck);
print(memories);
}
//instantiates memory object on click, renames each object with identifier and adds object to an Array
function OnClick () {
var clone : Rigidbody;
clone = Instantiate(memory, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.forward * throwPower);
clone.name = "clone" + memoryCounter.ToString();
memoryCounter++;
}
Your answer
Follow this Question
Related Questions
How do you make a clone have the same tag as its original? 0 Answers
Bullet clones will not be destroyed. 2 Answers
Random.range game object destroying itself before reaching destroy position.Please help. 1 Answer
Destroy button only destroys the last clone of object 1 Answer
Cloning an object seems to cause the original to disappear 1 Answer