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 Octillerysnacker · Nov 21, 2015 at 11:38 PM · variablesingleton

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

Alright, so I am trying to use singletons in place of static variables. I have my main script, singleton script and a manager script. I get the error shown in the title.

Main (JS):

 #pragma strict
 import System.Linq;
 
 var currentProjectile : Rigidbody;
 var launcher : Transform;
 var speed = 10.0;
 //TEXT
 var objText : UI.Text;
 var scaleText : UI.Text;
 public static var extensions = Manager.Instance.extensionHolder;
 public static var customs = Manager.Instance.customText;
 private var childrens = [];
 private var iterator = 0;
 private var rescaleFactor = 1.00;
 var objects : Rigidbody[];
 function Start () {
     objects = Resources.LoadAll("Blocks", Rigidbody).Cast.<Rigidbody>().ToArray();
     childrens = customs.GetComponentsInChildren(UI.Text);
 }
 
 function Update () {
     if(Input.GetKeyDown("q")){
         if(iterator+1 == objects.length){
             iterator = 0;
             currentProjectile = objects[iterator];
             objText.text = "Current Object: "+currentProjectile.gameObject.name;
         } else {
             iterator = iterator + 1;
             currentProjectile = objects[iterator];
             objText.text = "Current Object: "+currentProjectile.gameObject.name;
         }
     }
     if(Input.GetKeyDown("e")){
         // Instantiate the projectile at the position and rotation of this transform
         var clone : Rigidbody;
         clone = Instantiate(currentProjectile, launcher.position, launcher.rotation);
         clone.transform.localScale = new Vector3(rescaleFactor, rescaleFactor, rescaleFactor);
         clone.mass = clone.transform.localScale.x;
         // Give the cloned object an initial velocity along the current 
         // object's Z axis
         clone.velocity = transform.TransformDirection (Vector3.forward * speed);
     }
     if(Input.GetMouseButtonDown(1)){
         rescaleFactor = rescaleFactor + 0.25;
         scaleText.text = "Re-scale Factor: "+rescaleFactor;
     }
     if(Input.GetMouseButtonDown(0) && rescaleFactor != 0.25){
         rescaleFactor = rescaleFactor - 0.25;
         scaleText.text = "Re-scale Factor: "+rescaleFactor;
     }
 }
 
 function resText() {
     objText.text = "Current Object: "+currentProjectile.gameObject.name;
     scaleText.text = "Re-scale Factor: "+rescaleFactor;
     for (var child : UI.Text in childrens) {
      child.text = "(unused)";
     }
 }

Singleton(This script was borrowed and in C#):

 using UnityEngine;
 
 /// <summary>
 /// Be aware this will not prevent a non singleton constructor
 ///   such as `T myT = new T();`
 /// To prevent that, add `protected T () {}` to your singleton class.
 /// 
 /// As a note, this is made as MonoBehaviour because we need Coroutines.
 /// </summary>
 public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
 {
     private static T _instance;
 
     private static object _lock = new object();
 
     public static T Instance
     {
         get
         {
             if (applicationIsQuitting)
             {
                 Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
                     "' already destroyed on application quit." +
                     " Won't create again - returning null.");
                 return null;
             }
 
             lock (_lock)
             {
                 if (_instance == null)
                 {
                     _instance = (T)FindObjectOfType(typeof(T));
 
                     if (FindObjectsOfType(typeof(T)).Length > 1)
                     {
                         Debug.LogError("[Singleton] Something went really wrong " +
                             " - there should never be more than 1 singleton!" +
                             " Reopening the scene might fix it.");
                         return _instance;
                     }
 
                     if (_instance == null)
                     {
                         GameObject singleton = new GameObject();
                         _instance = singleton.AddComponent<T>();
                         singleton.name = "(singleton) " + typeof(T).ToString();
 
                         DontDestroyOnLoad(singleton);
 
                         Debug.Log("[Singleton] An instance of " + typeof(T) +
                             " is needed in the scene, so '" + singleton +
                             "' was created with DontDestroyOnLoad.");
                     }
                     else
                     {
                         Debug.Log("[Singleton] Using instance already created: " +
                             _instance.gameObject.name);
                     }
                 }
 
                 return _instance;
             }
         }
     }
 
     private static bool applicationIsQuitting = false;
     /// <summary>
     /// When Unity quits, it destroys objects in a random order.
     /// In principle, a Singleton is only destroyed when application quits.
     /// If any script calls Instance after it have been destroyed, 
     ///   it will create a buggy ghost object that will stay on the Editor scene
     ///   even after stopping playing the Application. Really bad!
     /// So, this was made to be sure we're not creating that buggy ghost object.
     /// </summary>
     public void OnDestroy()
     {
         applicationIsQuitting = true;
     }
 }
 Manager (C#):
 using UnityEngine;
 using System.Collections;
 
 public class Manager : Singleton<Manager>
 {
     public string myGlobalVar = "whatever";
     public GameObject extensionHolder ;
     public GameObject customText ;
 }
 


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

35 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

Related Questions

How to make a global variable in Unity? 5 Answers

[Help] Getting variable based on string value. 2 Answers

C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer

Variable keeps value after closing app 0 Answers

How do you add a variable to a text? 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