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
1
Question by Kionte · Jun 16, 2016 at 03:42 PM · c#nullreferenceexceptionnull reference exceptionruntime-generationclass object

Null Reference exception with custom class object

So I made a class called 'Dog' and made a constructor with it. the constructor contains some floats a int and a GameObject. I can create these 'Dog' elements at runtime, and the are stored in a list. but when I try to access them I get an error stating 'NullReferenceException: Object reference not set to an instance of an object' When I go to the inspector it shows the list and every time I hit 'd' it adds a element to the list. and is displayed in the inspector as Element 0 [Dog (none) ]. Which makes sense that it is empty and that this is causing the error. But because I am making the elements at run time I cant drag and drop a object into each element. How can I add a object to each element through the script so it can still function at runtime? Would what I just said solve my problem? Thanks!

Comment
Add comment · Show 4
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 Kionte · Jun 16, 2016 at 04:24 PM 0
Share
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using Game;
 
 public class SpecialFunctions : $$anonymous$$onoBehaviour {
     gotoFunction mGoToFunc;
     GameObject S1,S2,C1,C2;
     public List<Dog> dogs;
     public List<Human> humans;
     float X, Y, Z;
     int mId;
     public int getId() {mId++; return mId;}
 
     
     public GameObject randShape (string type) { // Generates the random 3-d object
         switch (type){
         case "Human":
             GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
             cube.transform.position = new Vector3 (randX (), randY (), randZ ());
             cube.transform.localScale = new Vector3 (1.5f, randHeight ("Human"), 0.75f);
             return cube;
             break;
         case "Dog":
             GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
             sphere.transform.position = new Vector3 (randX(), randY(), randZ());
             sphere.transform.localScale = new Vector3 (1.5f, randHeight ("Dog"), 0.75f);
             return sphere;
             break;
         default:
             print ("Not valid type.");
             return null;
         }
     }
     public void addLife() { // generates the enitre object 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.H))
             humans.Add (new Human (randAttack ("Human"), randHeight ("Human"), randRun ("Human"), randHealth ("Human"), randShape ("Human"), getId ()));
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.D))
             dogs.Add (new Dog (randAttack ("Dog"), randHeight ("Dog"), randRun ("Dog"), randHealth ("Dog"), randShape ("Dog"), getId ()));
     }
     public void checkForGoTo() { //checks if any objects are near eachother. if they are they goto() home base
         if (dogs.Count > 2) {
             for (int k = 0; k < dogs.Count - 1; k++) {
                 for (int i = k + 1; i < dogs.Count; i++) {
                     S1 = dogs [k].getShape ();
                     S2 = dogs [i].getShape ();
                     print (dogs [i].getAttack ());
                     if ((S1.transform.position.z - S2.transform.position.z < 2) &&
                        (dogs [k].transform.position.x - dogs [i].transform.position.x < 2)) {
                         mGoToFunc.goToStatic(dogs [k], dogs [i]);
                     }
                 }
             }
         }
     }
 
 }
avatar image Dibbie Kionte · Jun 16, 2016 at 06:22 PM 0
Share

Your code to me looks a little confusing, but to answer your question at first glance, when you create your actual Dog "prefab" or gameobject, you want to put that to a GameObject variable and add that variable to the list. $$anonymous$$ind of like this:

 Dog doggy =  new Dog (randAttack ("Dog"), randHeight ("Dog"), randRun ("Dog"), randHealth ("Dog"), randShape ("Dog"), getId ());
 
 dogs.Add(doggy);
avatar image Kionte · Jun 16, 2016 at 04:30 PM 0
Share

Here is my script im using a custom namespace to that is where Human and Dog come from. I cut out some of the functions because I past the character limit so the randAttack and stuff in the addLife() function are the ones that I deleted the problem is co$$anonymous$$g from the very last if statement it can not give me a current position because inside of the list is a empty Dog element. I tried accessing just the actual GameObject of the Dog object but I didnt accept that either

avatar image Kionte · Jun 16, 2016 at 04:54 PM 0
Share

When I print(); Dog.getShape(); or just Dog it outputs null. So I know for sure that it is null I just do not know how to make it not be null. especially because in the list the element is Type 'Dog' the class I created and is stroed in the list of type Dog. this line below adds to the list a Dog object with all of its attributes.: dogs.Add (new Dog (randAttack ("Dog"), randHeight ("Dog"), randRun ("Dog"), randHealth ("Dog"), randShape ("Dog"), getId ()));

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

166 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 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 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 avatar image

Related Questions

NullReferenceException when attempting to call a GameObject from within PointerEventData 1 Answer

(C#) Invoke giving null reference exception, crashing unity 0 Answers

Null Reference Exception Error 1 Answer

Faulty NullReferenceException? 2 Answers

Null Reference Exception on Sprite Renderer 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