Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Writing ScriptableObject from custom editor on game object

Discussion in 'Scripting' started by chilton, Jan 28, 2021.

  1. chilton


    May 6, 2008

    I have a game where NPC characters start at different locations and rotations in the game.

    I want to be able to move them around the level, whack a button, and save all of these positions and rotations to a ScriptableObject.

    I don't know how to make that work.

    I made a custom editor script but I have to click on the SO in the project tab to activate it, and then it has no scene context.

    I made a custom editor script for a game object, but I need it to work in the editor, not at play time. AND, I need it to access and modify a SO. So far when I try that, the numbers in the SO don't change.

    I looked into objects and parameters, but those seem to require a context I don't have, or I'm just doing something wrong.

    How, in general, do I do this?
  2. Kurt-Dekker


    Mar 16, 2013
    Assuming the SO is a bag holding all the relevant data, just ransack the scene looking for what you want to save.

    By "ransack the scene" I mean use any oneof the dozen or more ways to search for objects in Unity: find by tag, find by Component located on it, find by name, etc.

    Then iterate those, store the values you want from their Transform components, and that solves ONE direction.

    Now when the scene loads you will need a way to unambiguously associated Enemy X with the stored values for Enemy X, as there is no guarantee they will come in the same order. You can make a little script that sticks a GUID on each saved object so that it can re-find and re-associate them later.

    What you're proposing has a LOT of the same mechanisms as a game save/load system.

    Load/Save steps:
    chilton likes this.
  3. chilton


    May 6, 2008
    Thank you Kurt, that was solid advice.

    I ended up writing a editor that points to the SO, then I used GameObject.Find to track down the parts I wanted to store.

    easy peasy.

    Great advice!
    Kurt-Dekker likes this.