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 /
This question was closed Jul 16, 2019 at 10:13 PM by kico1603 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by kico1603 · Jul 16, 2019 at 07:21 PM · singleton

Singleton Class

Hello, good to everyone. I'm trying to do a singleton class to use it as static, I've seen thousands of examples and they all come out the same but to me, for some reason it does not work for me.

Class Singleton

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WorldGridGenerator : MonoBehaviour
 {
     public static WorldGridGenerator instance = null;
 
     private void Awake()
     {
         singleton();
     }
 
     private void singleton()
     {
         if (instance == null)
         {
             instance = this;
         }
         else if (instance != this)
         {
             Destroy(this.gameObject);
         }
         DontDestroyOnLoad(this.gameObject);
     }
 
     public static WorldGridGenerator Instance {
         get { return instance; }
     }
 
 
     public void generateGrid(GameObject gridCenter)
     {
 
 
     }
 }
 

Class that invoke it

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GridController : MonoBehaviour
 {
     //para debug
     public bool playerContain;
 
     private void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             WorldGridGenerator.Instance.generateGrid(gameObject);
             playerContain = true;
         }
     }
 
     private void OnCollisionExit(Collision other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             playerContain = false;
         }
     }
 }

Error:

NullReferenceException: Object reference not set to an instance of an object GridController.OnCollisionEnter (UnityEngine.Collision other) (at Assets/Scripts/Ponce/GridController.cs:14)

Line 14 : WorldGridGenerator.Instance.generateGrid(gameObject);

Thanks to everyone

Comment
Add comment · Show 1
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 Dragate · Jul 16, 2019 at 07:31 PM 1
Share

Is WorldGridGenerator class attached to any gameobject?

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Cornelis-de-Jager · Jul 16, 2019 at 09:38 PM

To fix your error you need to create an ingame object with your WorldGridGenerator attached.


Let me explain why, when creating a static class you can reference throught your code it looks like this normally:

 public static class WorldGridGenerator {
     ...
 }
 

But since we are inheriting from MonoBehaviour we can't instantiate static classes, only classes with static properties and functions.

 public class WorldGridGenerator : MonoBehaviour {
     
     public static WorldGridGenerator instance = null;
     
     ...
 }
 

Meaning that if you want to call the WorldGridGenerator as you did in your code you will first need to instiate a object with an instance of WorldGridGenerator already on it.

Comment
Add comment · Show 1 · 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 kico1603 · Jul 16, 2019 at 10:15 PM 0
Share

Thank you very much for your help

Follow this Question

Answers Answers and Comments

108 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

Related Questions

How do switch scene with singleton pattern unity3d C# 2 Answers

When I should use a singleton and when just GetComponent? 0 Answers

Unkown Identifier: 'Manager' error when trying to use singletons 0 Answers

Game Manager singleton doesn't sync across the network 1 Answer

Unassigned reference exception 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