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

    chilton

    Joined:
    May 6, 2008
    Posts:
    505
    Hi,

    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

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    31,139
    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:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384
     
    chilton likes this.
  3. chilton

    chilton

    Joined:
    May 6, 2008
    Posts:
    505
    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!
    -Chilton
     
    Kurt-Dekker likes this.