Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ReyzSchiffer · Jul 24, 2014 at 03:31 PM · listnull

Getting Null From List

I have 2 c# script, NPCManager and NPCs. NPCManager.cs using UnityEngine; using System.Collections;

 public class NPCManager : MonoBehaviour {
     public int npcID;
     public int npcStage;
     public string npcName;
     public string npcDialog;
 
     public NPCManager(int ID, int Stage, string Name, string Dialog)
     {
         npcID = ID;
         npcStage = Stage;
         npcName = Name;
         npcDialog = Dialog;
     }
 }
 

the NPCs.cs using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class NPCs : MonoBehaviour {
 
     public List<NPCManager> NPCList = new List<NPCManager>();
     
     void Start () {
 
         NPCList.Add( new NPCManager(1, 0, "test", "test" ) );
     }
 }
 

there is no error, no warning. in the inspector which NPCs added, when i start the game the list added 1 size but it say "None (NPCManager)".

i want it show the NPCManager variable.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xtro · Jul 24, 2014 at 04:01 PM

First of all, generally, you shouldn't use class constructors on classes derived from monobehaviour. And I don't get why you need that constructor at all.

Secondly, when you initiate a class in the member definition line, it's the same thing as writing it into the constructor which is wrong for monobehaviour.

Remove the NPCManager constructor and try this for NPCs class:

 public class NPCs : MonoBehaviour { 
     public List<NPCManager> NPCList;
 
     void Start () {
         NPCList = new List<NPCManager>();
         NPCList.Add( new NPCManager(1, 0, "test", "test" ) );
     }
 }
Comment
Add comment · Show 3 · Share
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
avatar image Xtro · Jul 24, 2014 at 04:02 PM 0
Share

The thing you did was good in the context of C# and .NET but not in Unity monobehaviour.

avatar image ReyzSchiffer · Jul 24, 2014 at 04:57 PM 0
Share

i know nothing about what you said. class derived, class constructors, behavour, member line ,etc.

can you post the NPC$$anonymous$$anager script so at least i know what constructor is.

i simply following this tutorial and convert from js to c# https://www.youtube.com/watch?v=rbW_gTwvTSA∈dex=2&list=PLLflfFtpkLT-ZunZu5nQ0pTspsT3GB9qS

avatar image Xtro · Jul 28, 2014 at 09:53 PM 0
Share

Constructor is the creator function of a class and it has the same class name. So in your script this is the constructor :

 public NPC$$anonymous$$anager(int ID, int Stage, string Name, string Dialog)
 {
     // ........
 }

Remove this function from NPC$$anonymous$$anager class and use the code I gave you for NPCs class.

I advice you to study Object Oriented Program$$anonymous$$g on C#. Without it, you won't be improving your self on program$$anonymous$$g.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Level generation using a "tree" type structure, why when i add a cell i get an error argument out of range? 1 Answer

Uninstantiated list somehow instantiated at runtime. 1 Answer

Null SerializedProperty SerializedObject upon Removal from list 1 Answer

List.Count returning 0 while it has 1 entry 2 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