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. Dismiss Notice

problem with a stack

Discussion in 'Scripting' started by gk104, Jun 26, 2016.

  1. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    Im just trying to push some values into a stack, but the program shows me an error and types:
    "Object reference not set to an instance of an object."

    the error below points to line 28 in the code below:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Menu_Stack : MonoBehaviour {
    6.  
    7.    public GameObject cube = new GameObject();
    8.    Stack<int> undoStack = new Stack<int>();
    9.  
    10.  
    11.     void OnGUI() {
    12.  
    13.         if (GUI.Button(new Rect(10, 70, 120, 30), "EXPAND CUBE SIZE"))
    14.         {
    15.             expand_and_add_to_stack(); // currently adds scale by 1 unit.
    16.         }
    17.  
    18.         if (GUI.Button(new Rect(10, 125, 120, 30), "UNDO"))
    19.         {
    20.            reduce_and_delete_from_stack();
    21.         }
    22.  
    23.     }
    24.  
    25.     void expand_and_add_to_stack()
    26.     {
    27.         cube.transform.localScale += new Vector3(1, 0, 0);
    28.         undoStack.Push(1);
    29.     }
    30.  
    31.     void reduce_and_delete_from_stack()
    32.     {
    33.       while(undoStack!=null)
    34.        {
    35.             cube.transform.localScale -= new Vector3(1, 0, 0);
    36.             undoStack.Pop();
    37.         }
    38.     }
    39.  
    40.  
    41. }
    42.  
     
  2. macorig

    macorig

    Joined:
    Dec 27, 2010
    Posts:
    52
    Are you initializing those two variables outside of Awake or Start? That shouldn't work, I think, and when I try to run your code it will actually throw an error for that reason. So maybe try something like this:

    Code (CSharp):
    1.     public GameObject cube;
    2.     Stack<int> undoStack;
    3.  
    4.     void Start () {
    5.         cube = new GameObject();
    6.         undoStack = new Stack<int>();
    7.     }
     
  3. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    I think the issue is with line 7:

    Code (CSharp):
    1.    public GameObject cube = new GameObject();
    It looks like Unity doesn’t want you to create GameObjects too early on.

    As far as I can tell, line 8 is actually okay:

    Code (CSharp):
    1.    Stack<int> undoStack = new Stack<int>();
    I think the undoStack variable wasn’t getting initialized because line 7 was throwing an exception.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Line 7 should just be public GameObject cube;
    Then in the inspector you should drag something into that slot. Or, if you are instantiating something, then you'll assign the value of that variable when you instantiate your object.