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 importguru88 · May 17, 2016 at 04:05 PM · scripting problemscript errorsingletondesign-patterns

How do I access Singleton Pattern Script in Unity3d C#

Okay. I got one script ' script in singleton pattern . I have an issue with the other script . I need to know what I need to change on the script call Game . I need to access the GameManagerSingleton from the game script is what I am saying. I know need to make some changes on it . I don't know how to approach this situation. I made some changes on the game script so I can access the singleton script . But I am getting a lot of errors. The game script access and keep track of the score and time . I just need the score code on the game script to be corrected. Here are my scripts :

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class GameManagerSingleton : MonoBehaviour { public static GameManagerSingleton instance = null;

  public int score = 0;
  Text text;
  void Awake()
  {
      if (instance != null && instance != this)
          Destroy(gameObject);    // Ensures that there aren't multiple Singletons
 
      instance = this;
  
  }
 

}

public class ScoreObject { void IncreaseScore() {

      GameManagerSingleton.instance.score += pointsEarned;
  }
 

void Update() {

       text.text = "Score : " + score;

}

}

using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using System.Collections; public class Game : MonoBehavior {

  private GameManagerSingleton GameManagerSingletonScript  _ Instance
  private myclock MyClockScript;
  
  private void Start () 
  {
     GameManagerSingleton = GetComponent<GameManagerSingleton>();
      myClock = GetComponent<MyClockScript>();
    }
 
  void Update () 
  {
      if (myClock != NULL && GameManagerSingleton != NULL)
      {
        // need somthing here
      }
        if (m_leftTime >= 0 &&  50 < GameManagerSingleton.score)
      {
          SceneManager.LoadScene("time");
      }
  }

}

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 Jessespike · May 17, 2016 at 05:29 PM 0
Share

format the code and proofread your question. It hardly makes sense.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessespike · May 17, 2016 at 05:58 PM

One of the features of singletons is that you can access the public static instance. There is no need for GetComponent and storing it in a separate class. You simply access the singleton with GameManagerSingleton.instance.

Is that a class inside of a class? Why is ScoreObject inside of GameManagerSingleton? How can IncreaseScore() use pointsEarned, if that pointsEarned isn't declared anywhere? Well, I'll ignore that because you only asked about Game script.

 public class Game : MonoBehavior {
 
       private myclock MyClockScript;
       
       private void Start () 
       {
          myClock = GetComponent<MyClockScript>();
       }
      
       void Update () 
       {
           if (myClock.m_leftTime <= 0 || GameManagerSingleton.instance.score > 50)
           {
               SceneManager.LoadScene("time");
           }
       }
 } 

  • Use "null", not "NULL". it's case-sensitive

  • No need to store a reference of GameManagerSingleton

  • m_leftTime is not declared anywhere, I'm assuming that's a property from MyClockScript.

  • GameManagerSingleton.score should be GameManagerSingleton.instance.score

  • I don't know what conditions you want for loading the "time" scene, but I'm going to guess you want to switch when time runs out or when the score is above 50, I'm not sure though, there are no comments describing the condition.

When writing scripts, it's better to write a little bit, compile it, check for errors and fix them immediately. Instead of writing 100s of lines and trying to fix all the errors at once.

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

60 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

Related Questions

How do I use Singleton Design pattern in unity3d C# 2 Answers

Singleton Problem 0 Answers

Something Is Breaking And I Don't Know What 0 Answers

i need help with my board manager coding ASAP 1 Answer

Help with Script 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