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 /
This question was closed Nov 19, 2016 at 09:09 PM by SgtShadowWalker.
avatar image
3
Question by SgtShadowWalker · Jun 24, 2016 at 11:27 AM · c#staticpublicvoidstartcoroutine

StartCoroutine in a public static void

Hi,

I'm writing a script for on-screen notifications that I want to call with a simple command from any other script using: "OnScreenNotification.ShowPopup ("Notification text.");"

This is my first time working with anything static and I'm kind of having trouble getting the hang of it. What I'm trying to do is start a coroutine whenever a script calls for OnScreenNotification.ShowPopup. The popup would then show up for a few seconds and then disappear again.

Sadly, I keep getting an error for StartCoroutine ("ShowOSN");

error CS0120: An object reference is required to access non-static member `UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)'

It does change the UI text, but the coroutine doesn't start. I've read a bunch of other questions from people about this problem, but so far none of them seemed to fix my problem.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class OnScreenNotification : MonoBehaviour {
 
     public static OnScreenNotification instance;
 
     public Image OSNBg;
     public Animator OSNAnim;
     public Text OSNText;
 
     public static bool canDisplay;
 
     public static void ShowPopup(string notificationtext) {
         GameObject.Find ("OSNText").GetComponent<Text> ().text = notificationtext;
         OnScreenNotification.canDisplay = true;
         StartCoroutine ("ShowOSN");
     }
 
     void Start () {
         OSNBg = GameObject.Find ("OnScreenNotification").GetComponent<Image> ();
         OSNAnim = GameObject.Find ("OnScreenNotification").GetComponent<Animator> ();
         OSNText = GameObject.Find ("OSNText").GetComponent<Text> ();
     }
     
     private IEnumerator ShowOSN () {
         OSNBg.enabled = true;
         OSNAnim.enabled = true;
         OSNText.enabled = true;
         yield return new WaitForSeconds (5);
         OSNBg.enabled = false;
         OSNAnim.enabled = false;
         OSNText.enabled = false;
     }
 }


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

1 Reply

  • Sort: 
avatar image
21
Best Answer

Answer by Bunny83 · Jun 24, 2016 at 11:41 AM

StartCoroutine is an instance method of MonoBehaviour. So you need an instance of any component to actually call StartCoroutine. So there's no way to use it directly inside a static method since a static method isn't bound to any instance.

Since that class seems to be some sort of singleton (because of your static "instance" variable) you can use this variable to start the coroutine. Furthermore you should avoid calling StartCoroutine with a string.

In your code it doesn't look like you initialize your "instance" variable anywhere. You have to initialize it inside Awake.

 void Awake()
 {
     instance = this;
 }
 
 public static void ShowPopup(string notificationtext) {
     instance.OSNText.text = notificationtext;
     canDisplay = true;
     instance.StartCoroutine (instance.ShowOSN());
 }

Comment
Add comment · Show 1 · 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 SgtShadowWalker · Jun 24, 2016 at 12:32 PM 0
Share

That works perfectly! Thank you for that and the clear explanation!

Follow this Question

Answers Answers and Comments

159 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 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 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

Start a Coroutine in script A, from script B C# 1 Answer

GetComponent C# 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Global Dynamic Arrays 2 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