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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to take data from a Class in a ScriptableObjects

Discussion in 'Scripting' started by White_Games, Apr 25, 2019.

  1. White_Games

    White_Games

    Joined:
    Feb 19, 2019
    Posts:
    6
    I have created two classes in a ScriptableObject:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(menuName = "State")]
    6. public class State : ScriptableObject
    7. {
    8.     [SerializeField] Context[] context;
    9.     [SerializeField] Decisions[] decisions;
    10.  
    11.     [System.Serializable]
    12.     public class Context
    13.     {
    14.         [TextArea(14, 10)] [SerializeField] string contextText;
    15.         [SerializeField] Sprite contextImage;
    16.     }
    17.  
    18.     [System.Serializable]
    19.     public class Decisions
    20.     {
    21.         [TextArea(3, 2)] [SerializeField] string decisionText;
    22.         [SerializeField] string jumpTo;
    23.     }
    24. }
    25.  

    I want to access to the data stored in the arrays, using a return method but i dont know how.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,390
    Just make all of the fields
    public
    . Is there a reason they are private?
     
    Ryiah likes this.
  3. White_Games

    White_Games

    Joined:
    Feb 19, 2019
    Posts:
    6
    Humm i thought all was public, but i dont know how to do that.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,390
    in C# all fields are private by default, you have to manually make them
    public
    .

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(menuName = "State")]
    6. public class State : ScriptableObject
    7. {
    8.     [SerializeField] public Context[] context;
    9.     [SerializeField] public Decisions[] decisions;
    10.  
    11.     [System.Serializable]
    12.     public class Context
    13.     {
    14.         [TextArea(14, 10)] [SerializeField] public string contextText;
    15.         [SerializeField] public Sprite contextImage;
    16.     }
    17.  
    18.     [System.Serializable]
    19.     public class Decisions
    20.     {
    21.         [TextArea(3, 2)] [SerializeField] public string decisionText;
    22.         [SerializeField] public string jumpTo;
    23.     }
    24. }
     
    Ryiah likes this.