How to acces variable from nested class with same name

Hi, I am asking just for curiosity. If i need use farmsoil timer in growingCrops constructor instead of growingcrops timer in line 39 what should i do.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class farmSoil : MonoBehaviour
{
    float timer;
    public List<growingCrops> currentCrops = new List<growingCrops>();
    public Transform[] cropLoc;
    public GameObject crop;
    public GameObject cropHarvested;
    float step= 16.53816f- 14.667f;


    private void FixedUpdate()
    {

        for(int i = 0; i < currentCrops.Count; i++)
        {
            currentCrops[i].timer -= Time.deltaTime;
        }
    
    }

    public void addCropsTilesToList()
    {
        foreach (Transform emptCropTile in cropLoc)
        {
           allObjects.emptyCropTiles.Add(emptCropTile);
        }
    }
    public class growingCrops
    {
        public float timer;
        public objectInfo myInfo;
        public Vector3 loc;
        public growingCrops(float timer,objectInfo myInfo,Vector3 loc)
        {
            this.timer = timer;  //this one i want to use farmsoil timer here
            this.myInfo = myInfo;
            this.loc = loc;
        }
    }
     void createWithRotation()
    {
       
        float angle = transform.rotation.eulerAngles.y; // Get rotation angle in radians
        Vector3 loc = Vector3.zero;
        Quaternion rot = Quaternion.Euler(0f, angle, 0f);
        for (int i = 0; i < 5; i++)
        {
            for (int a = 0; a < 5; a++)
            {
                Vector3 dist = new Vector3(i, 0f, a) * step;
                dist = rot * dist;
                float z = Mathf.Sin(angle);
                float x = Mathf.Cos(angle);
               // loc = initialPoint.position + dist;
                Instantiate(crop, loc, rot);
            }
        }
       
    }
    // Update is called once per frame

}

This is a nested class, it can’t access the members of the outer class’s instance. If the farmSoil->timer was constant or static, it could have been accessed by any class. Private nested types can’t be accessed from other classes, they can only be accessed from the outer class. Protected, public etc. nested types can be accessed from other classes like so:
var growingCropsInstance=new farmSoil.growingCrops(.. parameters..)
without a nested class you would do:
var growingCropsInstance=new growingCrops(.. parameters..)

Nested class, struct, enum definitions etc. are generally used for tightly defined types or for types that shouldn’t be used/accessed from external types. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/nested-types

What you should do is when calling the constructor of the growingCrops class, pass a mandatory float farmSoilTimer like so:

**public** growingCrops(**float** timer,objectInfo myInfo,Vector3 loc, float farmSoilTimer)

Then you can do **this**.timer = farmSoilTimer; in the constructor.

1 Like