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

Cannot access non-static variables from non-static method in struct

Discussion in 'Scripting' started by MLD_, Aug 13, 2022.

  1. MLD_

    MLD_

    Joined:
    Nov 2, 2021
    Posts:
    7
    I have a non-static method ComputeProbs in my Biome struct, but I get CS0120 on treeResolution and TreeIntervals. "An object reference is required for the non-static field, method, or property" The variable treeResolution and TreeIntervals method are both public and non-static.

    Code (CSharp):
    1.    [System.Serializable]
    2.     public struct Biome {
    3.        
    4.         public string name;
    5.  
    6.         [Tooltip("dirt: 0, moss: 1, rock: 2, sand: 3, snow: 5")]
    7.         public int[] textures;
    8.  
    9.         [Tooltip("Approx amount of trees in 1/4 chunk")]
    10.         public float treeDensity;
    11.  
    12.         public GameObject[] treePrefabs;
    13.         public BiomeSpawnConditions[] spawnConditions;
    14.  
    15.         [System.NonSerialized]
    16.         public float treeChance;
    17.  
    18.         [System.NonSerialized]
    19.         public float[] treeIntervals;
    20.  
    21.         public void ComputeProbs() {
    22.             treeChance = treeDensity * 4 / Mathf.Pow(treeResolution, 2);
    23.             treeIntervals = TreeIntervals(treePrefabs);
    24.         }
    25.     }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    It means TreeIntervals requires an object, you can't just call it like that.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    Are you missing a
    new
    on line 23?
     
  4. MLD_

    MLD_

    Joined:
    Nov 2, 2021
    Posts:
    7
    @RadRedPanda How would I call TreeIntervals()? I have called the function this way outside of the struct with no errors.
     
  5. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    I don't know, you didn't show what it looks like, so I have no idea what the method does.
     
  6. MLD_

    MLD_

    Joined:
    Nov 2, 2021
    Posts:
    7
    Sorry, here is the code:
    Code (CSharp):
    1.    float[] TreeIntervals(GameObject[] trees) {
    2.         int sum = 0;
    3.         foreach (GameObject tree in trees) {
    4.             sum += tree.GetComponent<Tree>().frequencyStrength;
    5.         }
    6.         float[] intervals = new float[trees.Length];
    7.         for (int i = 0; i < trees.Length; i++) {
    8.             intervals[i] = (float) trees[i].GetComponent<Tree>().frequencyStrength / sum;
    9.         }
    10.         for (int i = 0; i < trees.Length; i++) {
    11.             if (i > 0) {
    12.                 intervals[i] += intervals[i - 1];
    13.             }
    14.         }
    15.         return intervals;
    16.     }
     
  7. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Is TreeIntervals inside of a class? Inside the class, it will assume it's using itself as the reference. Your struct though, won't have a reference to that class.
     
    MLD_ likes this.
  8. MLD_

    MLD_

    Joined:
    Nov 2, 2021
    Posts:
    7
    Yes, TreeIntervals is inside of a class. The struct is inside of the same class, so I assumed that you could access the variables and methods normally.

    Thanks, I think I understand now.
     
  9. MLD_

    MLD_

    Joined:
    Nov 2, 2021
    Posts:
    7
    I updated my code, and it works!
    Thank you @RadRedPanda.