Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TonicMind · May 06, 2016 at 04:21 PM · nullqueueconstructordatastructure

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;
                 }
 
             }
         }
     }
 }

A picture of the code in the midst of being debugged, showing the null values on each data member

nullqueue.png (120.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

55 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges