C# error cs0120

public class GenerationManager : MonoBehaviour
{
public Stack Prefab1 = new Stack();

	public Stack<GameObject> Prefab2 = new Stack<GameObject>();
	
	public Stack<GameObject> Prefab3 = new Stack<GameObject>();
	
	public Stack<GameObject> Prefab4 = new Stack<GameObject>();

	public GameObject currentPrefab;

	private static GenerationManager instance;

	public static GenerationManager Instance
		 

	{
		get 
		{ 
			if (instance == null)
			{
				instance = GameObject.FindObjectOfType<GenerationManager>();
			}

			return instance; 
		
		}
	}

	// Use this for initialization
	void Start () 
	{
		createPrefabs (10);

		for (int i = 0; i < 4; i++)

		{
			SpawnPrefab ();
		}
	}
	// Update is called once per frame
	void Update () {
	
	}

	public void createPrefabs(int amount)
	{
		for (int i = 0; i < amount; i++)
		{
			Stack.Push(Prefab1)(Instantiate);

			Stack.Push(Prefab2)(Instantiate);

			Stack.Push(Prefab3)(Instantiate);

			Stack.Push(Prefab4)(Instantiate);

			Prefab1.Peek().gameObject.SetActive(false);

			Prefab2.Peek().gameObject.SetActive(false);

			Prefab3.Peek().gameObject.SetActive(false);

			Prefab4.Peek().gameObject.SetActive(false);

		}

	}
	public void SpawnPrefab()
	{
	

		if (Prefab1.Count == 0 || Prefab2.Count == 0 || Prefab3.Count == 0 || Prefab4.Count == 0) 
		{
			createPrefabs(5);
		}

		int randomIndex = Random.Range (0, 4); 

		if (randomIndex == 0) 
		{
			GameObject tmp = Prefab1.Pop ();
			tmp.SetActive (true);
			tmp.transform.position = currentPrefab.transform.GetChild (2).transform.GetChild (2).transform.GetChild (randomIndex).position;
			currentPrefab = tmp;
		}
		else if (randomIndex == 1) 
		{
			GameObject tmp = Prefab2.Pop ();
			tmp.SetActive (true);
			tmp.transform.position = currentPrefab.transform.GetChild (2).transform.GetChild (2).transform.GetChild (randomIndex).position;
			currentPrefab = tmp;
		} 
		else if (randomIndex == 2) 
		{
			GameObject tmp = Prefab3.Pop ();
			tmp.SetActive (true);
			tmp.transform.position = currentPrefab.transform.GetChild (2).transform.GetChild (2).transform.GetChild (randomIndex).position;
			currentPrefab = tmp;
		} 
		else if (randomIndex == 3) 
		{
			GameObject tmp = Prefab4.Pop ();
			tmp.SetActive (true);
			tmp.transform.position = currentPrefab.transform.GetChild (2).transform.GetChild (2).transform.GetChild (randomIndex).position;
			currentPrefab = tmp;
		}
		if (currentPrefab == null)
		{
			Debug.Log("currentPrefab == null");
			//return;
		}
		else if (currentPrefab.transform == null)
		{
			Debug.Log("currentPrefab.transform == null");
			//return;
		}
		else if (currentPrefab.transform.childCount < 3)
		{
			Debug.Log("currentPrefab.transform.childCount < 2");
			//return;
		}
		else if (currentPrefab.transform.GetChild (2).transform == null)
		{
			Debug.Log("currentPrefab.transform.GetChild (2).transform == null");
			//return;
		}
		else if (currentPrefab.transform.GetChild (2).transform.childCount < 3)
		{
			Debug.Log("currentPrefab.transform.GetChild (2).transform.childCount < 2");
			//return;
		}
		else if (currentPrefab.transform.GetChild (2).transform.GetChild (2).transform == null)
		{
			Debug.Log("currentPrefab.transform.GetChild (2).transform.GetChild (2).transform == null");
			//return;
		}
		else if (randomIndex >= currentPrefab.transform.GetChild (2).transform.GetChild (2).transform.childCount)
		{
			Debug.Log("currentPrefab.transform.GetChild (2).transform.GetChild (2).transform.GetChild (" + randomIndex + ").position");
			//return;
		}
	
	}
}

the error is in Stack.Push(Prefab1 - 4) (instantiate);

The declaration for Prefab1 is such that it is declared as a Stack of GameObjects. Similarly, Preb2-4 are also declared as stacks. There are four stacks of game objects declared. Perhaps that is the intent.
However, in method createPrefabs() the line:

 Stack.Push(Prefab1)(Instantiate);

has three potential issues:

  1. Push will accept any object as parameter. So it appears a Stack of GameObjects (Prefab1) is being pushed onto another stack. Again, this could be the intent.

2)The Stack needs to be assigned to a variable before it is used and its constructor is not a static method. This is the reason for the compiler CS0120 error. It cannot be used as a class method. Typically, it is used as:

Stack myStack = new Stack();
myStack.Push(myPrefabObject);
  1. Instantiate() is a unity method requiring parameters to be passed with, at minimum, the object to be instantiated, i.e. Instantiate(prefabObject). The following links provide more detail on object instantiation:

https://docs.unity3d.com/Manual/InstantiatingPrefabs.html

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

EDIT: Also, the lines similar to :

Prefab1.Peek().gameObject.SetActive(false);

will likely need to cast the returned Object from Peek() to a GameObject before being dereferenced:

((GameObject)Prefab1.Peek()).gameObject.SetActive(false);

or something similar.

Alright thanks for the help, i will try this out, and get back to you :slight_smile: