- Home /
Simple static function to display text onscreen (Accessible from anywhere)
Hi everybody,
I want to make a simple function that will accept a string and a number of second to display an information to the player. Responses in JS please. Hi want to be able to call it from any script. So far I created a GUItext object and attached the script below called Message.js
static function Affiche (mess , duree) {
Debug.Log("Fonction active!!!");
var component = gameObject.GetComponent(GUIText);
component.enabled = true;
component.text = mess;
yield WaitForSeconds(duree);
component.enabled = false;
}
and then I can call it like this from any script in my game
Message.Affiche ("Vous avez gagn",5);
I can see that the function is called because of the Debug.Log (if I put everything else in comments). I get the error:
Assets/SCRIPTS/Message.js(4,25): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'gameObject'.
I'm lost, help me please!
The question to ask would be, if the function Affiche() is static, what exactly do you expect 'gameObject' to point to?
Answer by Jean-Fabre · Jan 24, 2011 at 08:51 AM
Bonjour Jean-luc,
Ok, One way to do that and have access to normal functions like Yield is to use a singleton:
you can find the template for a singleton here
here is Message.js
// since we want to access a guiText, we make it mandatory to avoid errors @script RequireComponent( GUIText )
// based on http://www.unifycommunity.com/wiki/index.php?title=AManagerClass // /// Message is a singleton. /// To avoid having to manually link an instance to every class that needs it, it has a static variabe called /// instance, so other objects that need to access it can just call: /// Message.instance.DoSomeThing(); /// static var instance : Message;
// This is where the magic happens. // FindObjectOfType(...) returns the first AManager object in the scene. instance = FindObjectOfType(Message); if (instance == null) Debug.Log ("Could not locate an Message object. You have to have exactly one Message in the scene.");
// Ensure that the instance is destroyed when the game is stopped in the editor. function OnApplicationQuit() { instance = null; }
function Affiche(mess:String , duree:float) { Debug.Log("Fonction active!!!"); guiText.enabled = true; guiText.text = mess;
yield WaitForSeconds(duree); guiText.enabled = false;
}
create a guitext, and attach this script to it.
to test it, create another script that you attach to any gameobject in your scene
calltest.js function Update () {
if(Input.anyKeyDown){ Message.instance.Affiche("You have pressed "+Input.inputString,1); }
}
The trouble I had with creating a class with a static function is that yield wouldn't work, so I went for a singleton approach.
Hope it helps,
Jean
$$anonymous$$erci beaucoup $$anonymous$$, It's working... I'm still not sure if I understand everything but I will investigate further. By looking at your name I gess you speak french, but I'll speak english because I'm sure this answer may be usefull to someone else. Thanks again!
The principle might be difficult to grasp at first, but with practice it becomes a lot more obvious. Check the wiki link I gave and bounce from links to links, especially http://www.unifycommunity.com/wiki/index.php?title=Singleton
If I use this I'm assu$$anonymous$$g I still need to create a gameObject in the scene called '$$anonymous$$essage'? I want to be able to access a scripts functions without the need for it to be attached anywhere. How do I do that?
Your answer
Follow this Question
Related Questions
Accessing non static members in a static function argument?? 1 Answer
Can't EndGame from another Script: problems between PRIVATE and STATIC functions 2 Answers
Help with Error: An instance of type 'Regex' is required to access non static member 'Replace'. 2 Answers
Using a Function from another class without it being Static? 2 Answers
Static variables not working inside Function Update() 1 Answer