Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Jean-Luc Potvin · Jan 24, 2011 at 06:31 AM · functionstaticbce0020

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!

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 Jesse Anders · Jan 24, 2011 at 07:39 AM 0
Share

The question to ask would be, if the function Affiche() is static, what exactly do you expect 'gameObject' to point to?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · 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
avatar image Jean-Luc Potvin · Jan 24, 2011 at 12:36 PM 1
Share

$$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!

avatar image Jean-Fabre · Jan 24, 2011 at 01:26 PM 0
Share

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

avatar image grimmy · Nov 15, 2013 at 01:19 PM 0
Share

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

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

1 Person is following this question.

avatar image

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


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