Search Unity

Question Creating arbitrary number of variables using a script.

Discussion in 'Scripting' started by ylhs-262912, Feb 22, 2024.

  1. ylhs-262912

    ylhs-262912

    Joined:
    Oct 20, 2023
    Posts:
    2
    So, in my code, I use a singular script to run just about everything in my game. The thing is sometimes I need other objects to have extra variables on them. For example, on the object called obstacle, I have a script called obstacle properties which defines 2 variables: a Boolean called IsObstacle which I use instead of using unity's tag system, and an int called CurrentAnimation, and then reference (and update it) from my main script. The problem is, every time I create an object with a different set of variable to reference, I need a new properties script. I have a Player properties, Enemy properties, obstacle properties, Ground properties, etc... on, and on, and on. Is there any way to create a script that allows me to define a list of variables in the inspector with 1 script? These scripts literally do nothing but define new public variables, and I can't do it through the main script because i need to reference it per game object. This has been bugging me for days.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,797
    Gonna say no. Unity doesn't have a mechanism for this.

    I mean, you could use [SerializeReference] to serialize a collection with polymorphism, so a base interface/class (like empty) can be used, and derived types with any arbitrary data can be serialised into the collection. But you would of course be making lots of these derived types, plus an API to access them, plus the editor support. Which amounts to probably more code than your current situation.

    Nothing wong with having individual scripts that outline sets of properties.

    That said, this:
    Is setting off major alarm bells. This sounds like major 'god object' territory.
     
    Bunny83 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,656
    You can make a ScriptableObject-based system. That's what I did with my Datasacks package, which of course you're more than welcome to play with.

    20180724_datasacks_overview.png

    Datasacks is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/datasacks

    https://github.com/kurtdekker/datasacks

    https://gitlab.com/kurtdekker/datasacks

    https://sourceforge.net/projects/datasacks/

    If you are using any version of Unity later than Unity2021, it may be necessary to add this line to your Packages/manifest.json, or add it via the Package Mangler:

    "com.unity.ugui": "1.0.0",
     
    Bunny83 likes this.
  4. ylhs-262912

    ylhs-262912

    Joined:
    Oct 20, 2023
    Posts:
    2
    I didn't explain this well enough. I have a scene manager script with controls a list of events in the game. This can stop or start the functions of other scripts based on these triggers. For example, the player movement script will be turned off and the game over ui will be turned on when the game over trigger happens. when the player controller sees that it collides with a game over, it triggers the game over event in the scene manager script. The scene manager only does high level game logic, and controls most of the communication between scripts.
     
  5. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    703
    Please show us a concrete example of such variables and how you're setting/using them so we can understand better what you're trying achieve.
     
    lordofduct likes this.