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 vangivang · Jan 13, 2016 at 10:25 AM · gameobjectgamesingleton

Weird issue with Singleton

Hello everyone. Im quite new to Unity and C#. Been programming For Android for the passed few years.

I have a question about an issue Im encountering in my singleton class.

I have a Survival type game where you run around in a map and you shoot spawned enemies. To build the map I have a MapGenerator class. This class creates the map + obstacles. It also has a method that returns an empty cell (there is no obstacle in that cell).

I also have a Powerup mechanism that drops in a powerup at specific times. That happends using a PowerUpManager which is a singleton. That singleton has a method called DropHealthPowerup(). What it does first, is obtaining an empty cell using the MapGenerator and then it instantiates a 'full health' prefab in that cell. So to test this, I assigned a hot key 'J' to drop a health power up. It works perfectly.

Then, when the player dies somewhere in the game, I have the options to PLAY AGAIN. This button basically calls a method that calls Application.LoadLevel("main01"). This essentially loads the game again. But now, it seems, the MapGenerator object is null even though I do FindObjectOfType in the Awake method. When I debug this, in the Awake method _mapGenerator has a reference to a class. But when I press 'j' and the DropHealthPowerup() method gets called, _mapGenerator is null. It seems the _mapGenerator object inside the DropHealthPowerup() method is not the same object as in the Awake method. I can only assume the Destroy(gameObject) in the if statement in Awake has something to do with this mixup. This is the code for the singleton class:

 using UnityEngine;
 using System.Collections; 
 using System.Collections.Generic;

 public class PowerupManager : MonoBehaviour {

     public static PowerupManager _instance = null;

     [Header("Power ups")]
     public GameObject _healthPowerup;

     private MapGenerator _mapGenerator;

     void Awake(){

         if (_instance != null) {
             Destroy(gameObject);
         } else {
             _instance = this;
             DontDestroyOnLoad(gameObject);
         }

         _mapGenerator = FindObjectOfType<MapGenerator>();

     }

     public void DropHealthPowerup(){
         Vector3 fixedPosition = _mapGenerator.GetRandomOpenTile().position;
         fixedPosition += Vector3.up * 0.7f;
         Destroy(Instantiate(_healthPowerup, fixedPosition, Quaternion.identity), 5);
     }
 }

Any ideas what might be the issue? Thank you!

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

I have gotten all these errors when trying to script ? 2 Answers

Prevent gameobject go off screen 0 Answers

joystick android 3d , help. 0 Answers

How to make scoring system 1 Answer

Exporting Maya Animation of player with ball to unity ? 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