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 SoraMahiro · Dec 08, 2016 at 04:35 PM · c#rpgtetris

Unexplainable NullReferenceException?

So, making a tetris game, and trying to instantiate from a resources folder, here is the stack trace:

 namespace GameManager
 {
     public class GameManager:MonoBehaviour
     {
         Instantiator instance;
         void Start()
         {
             instance.Spawn();
         }
         public void CheckInput()
         {
             var posit = transform.position.y;
             if (posit != -10)
             {
                 if (Input.GetKeyUp(KeyCode.RightArrow))
                 {
                     transform.position += new Vector3(1, 0, 0);
                 }
                 else if (Input.GetKeyUp(KeyCode.LeftArrow))
                 {
                     transform.position += new Vector3(-1, 0, 0);
                 }
                 else if (Input.GetKeyUp(KeyCode.UpArrow))
                 {
                     transform.Rotate(0, 0, 90);
                 }
                 else if (Input.GetKeyUp(KeyCode.DownArrow))
                 {
                     transform.position += new Vector3(0, -1, 0);
                 }
             }
             else
             {
                 return;
             }
         }
         void Update()
         {
             CheckInput();
         }
     }
 }

Then,

 public interface Iinstantiatable
 {
     void SearchAndCall();
 }
 public class Instantiator:MonoBehaviour
 {
     Fire.Fire fireElement;
     Ice.Ice iceElement;
     Lightning.Lightning lightningElement;
     Terra.Terra terraElement;
     Water.Water waterElement;
     Wind.Wind windElement;
     public void Spawn()
     {
         System.Random RandomElement = new System.Random();
         int Case = RandomElement.Next(6);
         switch(Case)
         {
             case 1:
                 fireElement.SearchAndCall(); break;
             case 2:
                 iceElement.SearchAndCall(); break;
             case 3:
                 lightningElement.SearchAndCall(); break;
             case 4:
                 terraElement.SearchAndCall(); break;
             case 5:
                 waterElement.SearchAndCall(); break;
             case 6:
                 windElement.SearchAndCall(); break;
         }
     }
     void Start()
     {
         
     }
     void Update()
     {
 
     }
 }

Then, for example:

 namespace Fire
 {
     public class Fire:MonoBehaviour, Iinstantiatable
     {
         string TetroName;
         public void SearchAndCall()
         {
             GameObject Tetro = (GameObject)Instantiate(Resources.Load(ChooseFireShape(), typeof(GameObject)), new Vector2(0, 12f), Quaternion.identity);
         }
         public string ChooseFireShape()
         {
             System.Random RandomFire = new System.Random();
             int Case = RandomFire.Next(9);
             switch(Case)
             {
                 case 1:
                     TetroName = "FireTypes/I_Fire"; break;
                 case 2:
                     TetroName = "FireTypes/+_Fire"; break;
                 case 3:
                     TetroName = "FireTypes/Dot_Fire"; break;
                 case 4:
                     TetroName = "FireTypes/l_Fire"; break;
                 case 5:
                     TetroName = "FireTypes/LargeL_Fire"; break;
                 case 6:
                     TetroName = "FireTypes/Rect_Fire"; break;
                 case 7:
                     TetroName = "FireTypes/Sqr_Fire"; break;
                 case 8:
                     TetroName = "FireTypes/T_Fire"; break;
                 case 9:
                     TetroName = "FireTypes/U_Fire"; break;
             }
             return TetroName;
         }
         void Start()
         {
             
         }
         void Update()
         {
 
         }
     }
 }

The error occurs at GameManager.instance.Spawn().Following the trace back, I still don't see why there is a NullReferenceException. Any ideas why? Or maybe a bug in my code?

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 SoraMahiro · Dec 08, 2016 at 04:40 PM 0
Share

The folder "FireTypes" is in the Resources folder which is in the Assets folder, just to make that clear.

avatar image hexagonius · Dec 08, 2016 at 06:07 PM 0
Share

the instance variable of type Instantiator in the Game$$anonymous$$anager is private. This means it could not have been assigned to via inspector. Where do you create the actual object for this variable?

avatar image SoraMahiro hexagonius · Dec 08, 2016 at 06:45 PM 0
Share

The Instantiator variable is private because it comes from the public class Instantiator:$$anonymous$$onoBehaviour. It need not a creation or to be assigned via the inspector.

avatar image hexagonius SoraMahiro · Dec 10, 2016 at 10:21 AM 1
Share

that's the reason for the NullRef. just creating a variable for a reference type defaults to null.you need to assign a real object somehow. You could use the singleton pattern, or assign it the object in the inspector, or search for it with GameObject.Find,or... anyway, the variable needs a proper value

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SoraMahiro · Dec 10, 2016 at 07:07 PM

I actually found the problem. The problem wasn't about the variable not having a value, it was a problem of the variable not being able to take a value, what I mean by that is that I was calling a variable in this case instance without allowing a way to get the reference. My fix was using FindObjectOfType<Instantiator> . This allowed me to find the script on my Game Manager object. It also was used as FindObjectOfType<Fire> and as the other elements to be able to find those scripts, after that I had no problem Instantiating, but as game development goes, I came across another problem.

Comment
Add comment · 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

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

274 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 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

Unexplainable NullReferenceException? 0 Answers

Referencing instances of scripts based on the GameObject they are attached to. 0 Answers

Missing something fundamental about scripting an RPG party system - specifically, keeping characters in a list. 0 Answers

QuestSystem: the right way 0 Answers

How the heck do I save this info for my characters? [Serialization Usage Question] 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