Saving component -- what's the extra gameObject?

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
outofspace
Posts: 36
Joined: Wed Jul 03, 2013 7:28 am

Saving component -- what's the extra gameObject?

Post by outofspace »

Hi Joel,

Sorry for the many questions but it's EasySave integration time.

I followed the instructions on saving/loading a component.

The saving part is working. The loading is also working but something weird is going on.

public Transform trans1;
public Transform trans2;

ES2.Save(trans1, playerName+"trans1");
ES2.Save(trans2, playerName+"trans2");

// Now load our data in a similar way to how we saved it.
public Transform obj1;
public Transform obj2;
obj1 = ES2.Load<Transform>(playerName+"trans1");
obj2 = ES2.Load<Transform>(playerName+"trans2");

I assumed that the var obj1,obj2 would just get the references to saved trans1,trans2, which it did but then it went and created extra empty GameObjects.
Very non-intuitive behaviour because the saved trans1,trans2 are references to gameObjects that already exist in the scene. how can I get rid of these empty game objects?

Thanks.
outofspace
Posts: 36
Joined: Wed Jul 03, 2013 7:28 am

Re: Saving component -- what's the extra gameObject?

Post by outofspace »

Looks like the problem is that I did not use the "Self-Assigning" Load call for the Transform type.

The fix is to do:
ES2.Load<Transform>(playerName+"trans1",obj1);
ES2.Load<Transform>(playerName+"trans2",obj2);

Instead of :
obj1 = ES2.Load<Transform>(playerName+"trans1");
obj2 = ES2.Load<Transform>(playerName+"trans2");

Sorry, but the syntax is a bit misleading. Usually, the "=" assignment operator means to assign to the left-hand side the values of the right-hand side.
It seems that only the Transform type is handled in this way. In fact, ES2 handles Int,bool and other basic types in the normal way.
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving component -- what's the extra gameObject?

Post by Joel »

Hi outofspace,

In Unity, you cannot have a Component without a GameObject, therefore we can't Load and Create a Component without creating a GameObject to attach it to, unless the user provides a GameObject for us to attach it to :)

Regards,
Joel
Locked