Constructor always creates null object --why?
I've got a custom Queue data structure, it is being tested atm and that's why I'm here.
When I call a constructor for the Queue and its Node member class objects, it always returns null.
After some research I found that using the "new" keyword with things that derive from UnityEngine classes returns null, because you should be using Instantiate.
I found this page which lead me to believe that: http://stackoverflow.com/questions/28605788/constructor-returns-null-for-avatar-class
I guess that answers why I keep getting null values when the constructors get called. This Queue class uses generics, so at some point it may contain objects which do not derive from UnityEngine.
So my question is how can I test to make sure a value passed to a constructor is from UnityEngine or not?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
public class Utilities
{
/*Class to contain utilities for use in the game (and future games). QUEUE IS UNTESTED.
COULD USE A CONSTRUCTOR WHICH TAKES NO ARGS*/
public enum MOUNT_NORMAL { RIGHT, LEFT, UP, DOWN };
public class Queue<T>
{
Node<T> start;
Node<T> end;
int size = 0;
public Queue(T dat) //Queue constructor
{ //POP from the FRONT and PUSH on to the back!
start = new Node<T>(dat);
end = null; //End should always be null.
size += 1;
}
public Queue()
{
Type GameObject = T.GetType;
if(GameObject is T) {
}
start = new Node<T>();
end = null;
size = 0;
}
public void push(T newNodeData) //Takes data and puts it on the end of the queue
{
Node<T> newNode = new Node<T>(newNodeData); //Create a new node to add to the end of the queue
end.Next = newNode; //Put the new Node on the end.
end = end.Next; //Advance one
newNode.Next = null; //End the Queue
size += 1;
}
public T pop() //Returns the desired data instead of a reference to the (internal) Node
{
if (size > 1)
{
T dat = start.Data; //Store data for returning
start = this.start.Next; //Get next and make start that point to that value
size -= 1;
return dat;
}
return default(T);
}
public T Peek
{
get
{
return start.Data;
}
}
public int Size
{
get
{
return size;
}
}
private class Node<F>
{
F datum; //Data storage for this node
Node<F> next; //Reference to next in Queue data
public Node(F data) //Constructor
{
datum = data; //We do not change the data while the node exists. Create a new node if thats needed and reconnect things
next = new Node<F>(); //Next ought to be null at first because we only have one node and setting the next should be done outside the class using the Next property
}
public Node() //Constructor
{
datum = default(F); //We do not change the data while the node exists. Create a new node if thats needed and reconnect things
next = null;
}
public Node<F> Next //Property.
{
get
{
return next;
}
set
{
next = new Node<F>();
next = value;
}
}
public F Data //No actively changing Node data while in existence!
{
get
{
return datum;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Why do Constructors return null when used within EditorWindow 1 Answer
ArgumentException: The prefab you want to instantiate is null. 2 Answers
GetComponent returns null 0 Answers
Parsing a simple json object is always returning null properties,Parsing Always return empty object 1 Answer
How to use constructors (addComponent)? 0 Answers