StackOverflow problem

I need a variable that instantiate an object the first time that you call it. The variable is already part of a singleton.

public class Game_Manager : MonoBehaviour {

public static Game_Manager Instance{get; private set;}
    	
    	
    	
    	public WaypointRig waypointRig 
    	{
    		get
    		{
    			if (waypointRig == null) waypointRig = new WaypointRig();
    		
    			return waypointRig;
    		}

private set{}
    		
    	}
    }
}

But this code creates stackOverflow

You’re using the get property of waypointRig inside its own property. So you have waypointRig.waypointRig.waypointRig.waypointRig…, and thus stack overflow. Here you go :

private WaypointRig _waypointRig;
public WaypointRig waypointRig 
{
    get
    {
        if (_waypointRig== null) _waypointRig= new WaypointRig();
 
        return _waypointRig;
    }
 
    private set{ _waypointRig = value; }
 
}