Property Help

Hello,
Im taking a stab at setting up my first c# property but having some trouble.

Assets/Scripts/NodeScript/NodeTile.cs(26,18): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

Could someone please educate on what Im going wrong here. Thanks in advance

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


[System.Serializable]
public class NodeTile : NodeBase {


    private NodeAttrActivePrefab _nodeAttrActivePrefab;                                              
    public  NodeAttrActivePrefab  nodeAttrActivePrefab{get {return _nodeAttrActivePrefab;}}
    private NodeAttrPool _nodeAttrPool;                                                              
    public  NodeAttrPool nodeAttrPool{get {return _nodeAttrPool;}}

    //
    // Constructor running base class constuctor
    public NodeTile(Vector3 worldPosition, int gridX, int gridY)
        : base( worldPosition,  gridX,  gridY) {
           
        //
        // TEST Pooling
        _nodeAttrPool.PoolObject = GameObject.Find ("Cube").name;
        _nodeAttrPool.PoolGroup.NextPoolObj;

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

public struct NodeAttrPool  {

    private string _poolObject;                              
    public string PoolObject
    {
        get
        {    //
            // Return from private
            return _poolObject;
        }
        set {
            //
            // Set poolName
            PoolName = value;
            //
            // Return to private
            _poolObject = value;
        }
    }
    private string _poolName;                              
    public string PoolName
    {
        get
        {    //
            // Return from private              
            return _poolName;
        }
                          
        set
        {  
            //
            // Create the GOPoolGroup
            if (GameObject.Find (value + "Pool" ) == null) {
                GameObject currentGoPoolGroup = new GameObject (value + "Pool");
                Pool currentPoolGroup = currentGoPoolGroup.AddComponent<Pool> ();
                currentPoolGroup.poolObj = GameObject.Find (value);
                //
                // Set PoolGroup
                PoolGroup = currentPoolGroup;
              
            }
            //
            // Return to private
            _poolName = value + "Pool" ;
        }
    }
    private Pool _poolGroup;                                  
    public Pool PoolGroup
    {
        get
        {    //
            // Return from private
            return _poolGroup;
        }
        set
        {    //
            // Return to private
            _poolGroup = value;
        }
    }
}
 using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Pool : MonoBehaviour {


    public GameObject poolObj;                                          // Object we want to pool
    public List<GameObject> pooledObjects = new List<GameObject>();        // List of inactive pool objects
    public GameObject NextPoolObj {                                        // GameObject clone = pool.nextThing; triggers get{} & pool.nextThing = gameObject; triggers set{}
        get {
            //
            // Check if we have any avaliabel pooled objects
            if (pooledObjects.Count < 1){

                //
                // If not clone a new one
                GameObject newClone = (GameObject)Instantiate (poolObj);
                newClone.transform.parent = transform;
                newClone.SetActive (false);
                pooledObjects.Add (newClone);

                //
                // Add poolMember script componet to clone, the pool memeber
                // calls pool.nextThing = gameObject; in void OnDisable() whitch
                // fires off set{} and retuns obj to pool.
                PoolMember poolMember = newClone.AddComponent<PoolMember>();
                poolMember.pool = this;

            }
            //
            // If we have an object avaliable in pooledObjects list set it to active, remove from pooledObjects and retun
            GameObject clone = pooledObjects[0];
            pooledObjects.RemoveAt (0);
             clone.SetActive (true);
            return clone;
        }
        set {
            //
            // Returns the object back to pooledObjects by calling  pool.nextThing = gameObject; in void OnDisable()
            value.SetActive (false);
            pooledObjects.Add (value);
        }
    }
}

The error points to line 26, but your posted NodeTile code has fewer lines than that. What line is that error actually referencing?

Sorry was missing my spaces, all fixed.

It on this line calling ,my nextPoolObj “_nodeAttrPool.PoolGroup.NextPoolObj;”

NextPoolObj is a property. If you’re calling it you need to explicitly set or get it.

E.g.

var nextObj = _nodeAttrPool.PoolGroup.NextPoolObj;

or

_nodeAttrPool.PoolGroup.NextPoolObj = myGameObj;

The issue is that you have “NextPoolObj” sitting on a line on its own. You need to assign that to something. (If you don’t have anything to assign it to, then why would you be calling it?)

GameObject someObject = _nodeAttrPool.PoolGroup.NextPoolObj;

I will add this: I don’t think that functionality works well as a property. For one, as a general “code style” rule, it’s probably a bad idea for properties to have that much code in them. Properties should be pretty lightweight. They’re convenience features that replacing accessing a member variable, and as such, coders who see them generally make at least two assumptions about it that this property does not adhere to:

  1. It’s lightweight, fast code that I can execute without worrying much about performance costs;
  2. If I will the same property getter twice, I will probably get the same value back both times (like a variable).

If I saw this property in a project I was collaborating on, I would think it was Really Weird ™ and would have expected this to be a function instead.

I see ok, thanks for the help, im going to turn this into a funtions

Really appreciate all the help guys

1 Like