Instantiate for storage -- don't display

Hey everyone,

I'm doing some basic experimentation with the engine for a possible future game that would involve some time-warping aspects (e.g. Prince of Persia). Essentially, I want to be able to save the state of a Game Object (in this case, the Player), store it, and then recall it if needed.

I seem to be able to do this with

var Copy_of_Player = Instantiate(Player_Object)

However, the one problem with this is that as soon as the object is Instantiated, it is activated (all the scripts begin to run) and rendered. I seem to be able to do:

Copy_of_Player.active = false;

To stop the scripts/movement/etc. However, when I also try to do:

Copy_of_Player.renderer.enabled = false;

I get an error that there is no renderer attached to Copy_of_Player. (Actually, the exact Debug message says "No renderer attached to Player(Clone)")

This is a top-level Game Object in the hierarchy...it has scripts as well as several meshes. What am I doing wrong?

In addition, if anyone can point out a better way to save the state of a Game Object for later use (I basically want to be able to put all the main components of a level that are within range back to the way they were 5 seconds ago), that would be greatly appreciated.

Thanks!

A naive brute force approach that should work is to create a Memento object (class, struct or even array if you like) that stores just the variables in question.

Something like memento.storeState(Player_Object); and then memento.applyState(Player_Object). If you add more variables that you want saved, you'd have to add them to the memento and its functions, but this should work.

Unity js

class Memento
{
    var foo : Type1;
    var bar : Type2;

    function storeState(player_Object : GameObject)
    {
        var script : ScriptName = player_Object.GetComponent(ScriptName);
        foo = script.foo;
        bar = script.bar;
    }

    function applyState(player_Object : GameObject)
    {
        var script : ScriptName = player_Object.GetComponent(ScriptName);
        script.foo = foo;
        script.bar = bar;
    }
}

Unity mono

class Memento
{
    Type1 foo;
    Type2 bar;

    void storeState(GameObject player_Object)
    {
        ScriptName script = player_Object.GetComponent<ScriptName>();
        foo = script.foo;
        bar = script.bar;
    }

    void applyState(GameObject player_Object)
    {
        ScriptName script = player_Object.GetComponent<ScriptName>();
        script.foo = foo;
        script.bar = bar;
    }
}

Obviously if you wanted to access variables stored in the transform or anywhere else in the object, you could do that too.

To implement an actual memento pattern, you would in stead have a class representing the state and you would then apply that rather than directly tweaking each variable. The Player_Object would contain an instance of this state as would the memento and the memento's functions would never change they'd just be `state = script.state` and `script.state = state`. Then, whenever you wanted to add variables to the state, you would simply add them to the state class and the memento would just work without changes. But with this implementation, you could also cut out the Memento completely and just store an instance of the state class. I don't recall off the top of my head whether the Unity js treats user-defined classes as reference types and if it does, this implementation may require you to implement a deep copy function (clone, assignment operator overload, etc.)