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