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

Getting Scriptable object properties returns null

Discussion in 'Scripting' started by Fluffysnails46, Apr 6, 2022.

  1. Fluffysnails46

    Fluffysnails46

    Joined:
    Jan 15, 2021
    Posts:
    52
    I am working on generating 3D planets, and i have a class set up inside of a scriptable object. On accessing it, the class's values return null.
    Class:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu()]
    6. public class PlanetSettings : ScriptableObject
    7. {
    8.     public float planetRadius = 1;
    9.     public NoiseLayer[] noiseLayers = new NoiseLayer[1];
    10.  
    11.     [System.Serializable]
    12.     public class NoiseLayer
    13.     {
    14.         public bool enabled = true;
    15.         public bool useFirstLayerAsMask;
    16.         public NoiseSettings noiseSettings;
    17.     }
    18. }
    19.  
    Main Code:
    Code (CSharp):
    1.         p.planetSettings.noiseLayers[0].noiseSettings.center = new Vector3(Random.Range(-1000,1000),Random.Range(-1000,1000),Random.Range(-1000,1000));
    2.         p.planetSettings.noiseLayers[0].noiseSettings.layers = 6;
    3.         p.planetSettings.noiseLayers[0].noiseSettings.strength = 2;
    4.         p.planetSettings.noiseLayers[0].noiseSettings.baseRoughness = .25f;
    5.         p.planetSettings.noiseLayers[0].noiseSettings.roughness = 2;
    6.         p.planetSettings.noiseLayers[0].noiseSettings.persistence = .5f;
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    You need to break up that line to narrow down which item in that long, long line is the one causing the exception. Right now I count 5 different parts of the line that could be null and cause this error, each of which implies a different solution. Is "p" null? Is "planetSettings" null? is "noiseLayers" null? etc - check each one.
     
  3. Fluffysnails46

    Fluffysnails46

    Joined:
    Jan 15, 2021
    Posts:
    52
    its noiseLayers, ive done some debug.Log()
     
  4. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    The whole of noiseLayers or noiseLayers[0]?
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Are you initialising your NoiseLayer class at index 0 of the array?
     
  6. Fluffysnails46

    Fluffysnails46

    Joined:
    Jan 15, 2021
    Posts:
    52
    Noiselayers[0] is null, and what exactly do you mean by intitialized?
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    public NoiseLayer[] noiseLayers = new NoiseLayer[1];
    just makes an array with a length of 1. It doesn't instantiate an instance of NoiseLayer in its index however. You, at some point, need to
    noiseLayers[0] = new NoiseLayer();
    or whatever your constructor is for it.
     
  8. Fluffysnails46

    Fluffysnails46

    Joined:
    Jan 15, 2021
    Posts:
    52
    So I forgot to add noiseLayers[0] = new NoiseLayer(), but now the code thinks it dosent exist even though i typed it correctly, so im getting a error message that says "Assets\Planet.cs(26,47): error CS0246: The type or namespace name 'NoiseLayer' could not be found (are you missing a using directive or an assembly reference?)"

    Edit: nevermind, i fixed it, but the game is doing the old error message