How can i use a custom script once on a saved prefab.

Discussion and help for Easy Save 3
Post Reply
cragggle
Posts: 11
Joined: Mon Jun 10, 2019 4:05 am

How can i use a custom script once on a saved prefab.

Post by cragggle »

Basically, I have a "heavy object" script.
When one of my heavy objects spawns in, the script talks to another script and tells it to add a force to all objects around it.

The heavy object script is attached to my prefab. I want to be able to disable/remove the script after OnCollisionEnter so it only happens once, then when I close/save the game and reopen I don't want it to apply the force again.

Code: Select all

    
void OnCollisionEnter(Collision collision)
    {
      wind.Jolt(); //Myscript and function.
      Destroy(this);
    }
Destroy(this);
only works during the runtime but the script is still appearing when it reloads.
I also tried
this.enabled = false;
After I created a typescript for my HeavyObject script, however, this.enabled does in fact disable the script however it does not stop the force from happening like Destroy(this) does.

Is there anything I can do in the custom types window to only have it check once or remember that it's been destroyed?

Thanks!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How can i use a custom script once on a saved prefab.

Post by Joel »

Hi there,

The easiest way to do this would be to modify the script so that it saves a bool just before you destroy it indicating that it needs destroying, and then load this on Start() and destroy the script if necessary. For example:
public YourScript : MonoBehaviour
{
    public void Start()
    {
        if(ES3.Load<bool>(ES3ReferenceMgr.Current.Get(this).ToString(), false))
            Destroy(this);
    }

    void OnCollisionEnter(Collision collision)
    {
      wind.Jolt(); //Myscript and function.
      // Save a bool indicating that we want to destroy this script on next load.
      // We use ES3ReferenceMgr.Current.Get(this).ToString() to get the unique ID for this script.
      ES3.Save<bool>(ES3ReferenceMgr.Current.Get(this).ToString(), true);
      Destroy(this);
    }
}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
cragggle
Posts: 11
Joined: Mon Jun 10, 2019 4:05 am

Re: How can i use a custom script once on a saved prefab.

Post by cragggle »

Hi, thanks for replying, this is really cool and is very close to a solution.

I had to first put the check to destroy on Awake() as it was still calling Jolt() first for some reason.

Have 2 problems now.
When I save and play again, firstly even though the object is disabled, It's still trying to access my other script
with

Code: Select all

wind = GameObject.Find("WindZone").GetComponent<Wind>();
But that line of code isn't being called anymore. Tried putting that in awake too but it didn't work.

And lastly, I'm getting

Code: Select all

IOException: Sharing violation on path "MY APPDATA PATH WITH SAVEDATA.ES3"
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean useAsync) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
ES3Internal.ES3FileStream..ctor (System.String path, ES3Internal.ES3FileMode fileMode, System.Int32 bufferSize, System.Boolean useAsync) (at Assets/Plugins/Easy Save 3/Scripts/Streams/ES3FileStream.cs:12)
(wrapper remoting-invoke-with-check) ES3Internal.ES3FileStream..ctor(string,ES3Internal.ES3FileMode,int,bool)
ES3Internal.ES3Stream.CreateStream (ES3Settings settings, ES3Internal.ES3FileMode fileMode) (at Assets/Plugins/Easy Save 3/Scripts/Streams/ES3Stream.cs:24)
ES3Reader.Create (ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:308)
ES3.Load[T] (System.String key, T defaultValue, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:306)
ES3.Load[T] (System.String key, T defaultValue) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:278)
HeavyObject.Awake () (at Assets/Scripts/HeavyObject.cs:12)
UnityEngine.Object:Instantiate(GameObject)
ES3Types.ES3Type_ES3PrefabInternal:Read(ES3Reader) (at Assets/Plugins/Easy Save 3/Scripts/ES3Prefab.cs:131)
ES3Reader:ReadObject(ES3Type) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:215)
ES3Reader:Read(ES3Type) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:244)
ES3Types.ES3Type_GameObject:ReadObject(ES3Reader) (at Assets/Plugins/Easy Save 3/Scripts/Types/Unity Types/ES3Type_GameObject.cs:106)
ES3Types.ES3ObjectType:Read(ES3Reader) (at Assets/Plugins/Easy Save 3/Scripts/Types/ES3ObjectType.cs:40)
ES3Reader:ReadObject(ES3Type) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:215)
ES3Reader:Read(ES3Type) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:244)
ES3Types.ES3ArrayType:Read(ES3Reader) (at Assets/Plugins/Easy Save 3/Scripts/Types/Collection Types/ES3ArrayType.cs:57)
ES3Reader:Read(ES3Type) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:240)
ES3Reader:Read(String, GameObject[]) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:178)
ES3:Load(String, GameObject[], ES3Settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:310)
ES3AutoSaveMgr:Load() (at Assets/Plugins/Easy Save 3/Scripts/Auto Save/ES3AutoSaveMgr.cs:42)
ES3AutoSaveMgr:Awake() (at Assets/Plugins/Easy Save 3/Scripts/Auto Save/ES3AutoSaveMgr.cs:56)

Appreciate the help alot.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How can i use a custom script once on a saved prefab.

Post by Joel »

Hi there,

Would you be able to provide me with a standalone script and instructions to replicate this? Also if you're not using the latest version of Easy Save, please make sure you update and see if that resolves the issue.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply