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
-1
Question by asdf123 · Apr 30, 2014 at 09:51 PM · listnullreferenceexceptionstaticaddgeneric

Add to generic list on start problem

I'm doing tests with generic list so dont care about "Pokemon scripts" and don't bother me about copyright and etc, that just for testing purposes..

So, i want that when a pokemon spawns it add to list his name, id and game object, pokemon script:

 void Start () {
         GameController.instance.AddPokemonToList(GameController.instance.pokemons.Count + 1, this.gameObject.name, this.gameObject);
     }


and then, on the GameController script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class Pokemon {
     public int id;
     public string name;
     public GameObject go;
 }
 
 public class GameController : MonoBehaviour {
 
     public List<Pokemon> pokemons;
 
     public static GameController instance;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
     }
     public void AddPokemonToList(int _id, string _name, GameObject _go) {
 
         Pokemon _pokemon = new Pokemon {
 
             id = _id,
             name = _name,
             go = _go
 
         };
 
         instance.pokemons.Add (_pokemon);
 
     }
 }



The console gives me an error on the pokemon script, null reference exeption. What's the problem ?

Comment
Add comment · Show 2
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 markedagain · Apr 30, 2014 at 09:54 PM 1
Share

i see that you are trying to do singlton patern, but when do you actually load the variables with the objects ? i also dont see new List() anywhere

avatar image asdf123 · Apr 30, 2014 at 10:35 PM 0
Share

not work yet :/

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by wibble82 · Apr 30, 2014 at 09:58 PM

You aren't creating the list, so it is null.

You'll need pokemons = new List();

Either put it in the start function, or just after the member variable declaration.

FYI that new List line will need point brackets with Pokemon in the middle, but my iPad won't let me enter that text for some reason!

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 asdf123 · Apr 30, 2014 at 10:20 PM 0
Share

dont work yet

avatar image wibble82 · Apr 30, 2014 at 10:47 PM 0
Share

Well that's definitely the problem I'm afraid. Did you out it after the member variable declaration like I said?

avatar image wibble82 · Apr 30, 2014 at 10:49 PM 0
Share

Oh I just noticed - the static game controller isn't being set either! You're trying to do a singleton pattern which is a bit more complex in unity. Check out this tutorial: http://clearcutgames.net/home/?p=437

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

22 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

Related Questions

Javascript Array to Generic List 1 Answer

Iterate through Variables in a Script and adding them to a Generic List 1 Answer

Null reference exception in Lists 1 Answer

A node in a childnode? 1 Answer

Remove and Add to List By Name aad 1 Answer


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