Search Unity

C# Scriptable Objects - Trouble adding it at runtime

Discussion in 'Scripting' started by bigboybifdick, Aug 27, 2015.

  1. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hi,

    I'm currently looking at how Scriptable Objects work.
    I have created a Scriptable Object via Script.

    Now i would like to add it to a GO at runtime.
    At Start i instantiate myPlayerGO, then i'm adding a script to it. I would like this script to be able to access the data stored in my scriptable Object.

    I have no problem doing it when i drag the ScriptableObject in the editor. But it doesn't seem to work when i try doing it directly from the script.

    Here is how im doing it :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MonsterSelectPlatform : MonoBehaviour {
    5.     public ScriptableObject DataManager;
    6.     private int Xposition;
    7.     private int Zposition;
    8.  
    9.     void Awake(){
    10.  
    11.         ScriptableObject DataManager = ScriptableObject.CreateInstance<InstantiationData> () as ScriptableObject;
    12.     }
    Problem is that further i'm not able to access it.

    Code (CSharp):
    1. void Update () {
    2.         GameObject Selector = GameObject.FindGameObjectWithTag("Selector");
    3.         Selector.transform.position = new Vector3 (Xposition,0,Zposition);
    4.  
    5.         if(Input.GetKeyDown("z") && Selector.transform.position.z[B]DataManager.GridSizeZ && ICanMove == true){
    6.             StartCoroutine("SelectionDelay");
    7.         } }
    Unity says that there is no GridSizeZ value in DataManager.

    Of course this value exists :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. [System.Serializable]
    7. public class InstantiationData : ScriptableObject  {
    8.  
    9.     public int NbOfPlayer = 4;
    10.     public int GridSizeX = 4;
    11.     public int GridSizeZ = 4;
    12.     }
    13.  
    14.  
    And if i remove the GridSizeZ from the first script, i can see in the editor that my Scriptable Object is correctly added to my GO :



    Well, im just beginning with the Scriptable Objects, so i probably don't really get it all...
    What's is my mistake ? :)

    Thanks !!
     
    Last edited: Aug 27, 2015
  2. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hi again,

    I tried without creating an instance, just loading it from the resources folder :
    Code (CSharp):
    1. void Awake(){DataManager = Resources.Load("DataInstantiation") as ScriptableObject;}
    It's added correctly, but i can't access the data either...

    "`UnityEngine.ScriptableObject' does not contain a definition for `GridSizeZ'"
     
    Last edited: Aug 27, 2015
  3. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    your variable type needs to be InstantiationData instead of ScriptableObject.