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 /
avatar image
3
Question by dibonaj · Jul 06, 2011 at 02:20 PM · c#staticienumeratorlibrary

public static IEnumerator

Hi all. What I am basically trying to do is take a bunch of functions out of one of my scripts and put them into their own script, basically turning them into my own little library. For the most part I have gotten this to work. The only thing I can't seem to get to work is the IEnumerator. I have figured out that I can't call it with StartCoroutine as that appears to be a non-static member trying to call a static member. I think I have the call right, the problem is the raycast inside of it. Since this script isn't attached to anything, I'm not 100% sure the raycast I have inside of it is doing anything. Any suggestions on the direction I should head in would be greatly appreciated. Code follows:

 public static IEnumerator DisableInput(GameObject[] gos, RaycastHit hitItem)
 {
     LearningObject snd;
     snd = hitItem.collider.gameObject.GetComponent("LearningObject") as LearningObject;
     float soundDuration = snd.audio.clip.length;
     
     if(snd.hasSound && snd.audio.isPlaying)
     {    
         for(int i = 0; i < gos.Length; i++)
         {                        
             gos[i].layer = 2;
             print("You are now on the ignore raycast layer");
         }
         
         yield return new WaitForSeconds(soundDuration);
         
         for(int j = 0; j < gos.Length; j++)
         {
             gos[j].layer = 0;
         }
         print("You are back on the default layer.");
     }
 }
 
 public static void LoopThroughObjects(GameObject[] gos, ArrayList myList, RaycastHit hitItem, MonoBehaviour instance)
 {
     for(int i = 0; i < gos.Length; i++)
     {
         print("The count of the arraylist is now at: " + myList.Count);
         print("The object that was just clicked is: " + hitItem.collider.gameObject.name);
     
         if(hitItem.collider.gameObject == gos[i])
         {
             bool foundit = false;
             
             for(int j = 0; j < myList.Count; j++)
             {
                 print("i equals " + i + " and j is " + j);
                 if(gos[i] == myList[j])
                 {
                     foundit = true;
                     print("Found it is now set to " + foundit );
                     break;
                 }
             }
             
             if(!foundit)
             {
                 myList.Add(hitItem.collider.gameObject);
                 print("The items in the array are now " + myList.Count);
                 instance.StartCoroutine(DisableInput(gos, hitItem));
             }
         }
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

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

Answer by Bunny83 · Jul 06, 2011 at 02:34 PM

Coroutines actually run on MonoBehaviour instances, so you need to pass a reference of such an instance to your LoopThroughObjects function that is used to run the corouting on.

Besides that, watch out because "print" is only available in classes that derives from MonoBehaviour. I use always Debug.Log since it works everywhere.

It's no problem to call a static coroutine, but StartCoroutine is a member function of MonoBehaviour so you need an instance ;)

edit

 public static void LoopThroughObjects(GameObject[] gos, ArrayList myList, RaycastHit hitItem, MonoBehaviour instance)
 {
     [...]
     instance.StartCoroutine(DisableInput(gos));
     [...]
 }

To call this function from another script (which is a true instance of a class that is derived from MonoBehaviour) do:

 MyLibrary.LoopThroughObjects(goArray, myList, hit, this);

this will give you the reference to the instance of the class you're actually in.

Comment
Add comment · Show 7 · 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 Bunny83 · Jul 06, 2011 at 02:45 PM 0
Share

Just some further hints:

  • I would replace the slow untyped ArrayList with a List<GameObject>

  • Your whole inner for-loop can be replaced by myList.Contains( hitItem.collider.gameObject ) which returns true when it's in the list ;)

avatar image dibonaj · Jul 06, 2011 at 02:48 PM 0
Share

I've not done anything like that before (or if I have I am unaware of it). Can you give an example or point me in the right direction of the unity reference for passing a reference of that monobehaviour instance.

Also, thanks for the warning on print(). I will keep that in $$anonymous$$d.

avatar image dibonaj · Jul 06, 2011 at 03:08 PM 0
Share

Thank you very much. I updated my code to show the working version. I appreciate the help.

avatar image dibonaj · Jul 06, 2011 at 06:07 PM 0
Share

I would also like to note that I followed your suggestion and went with the generic list ins$$anonymous$$d of the arrayList. Got rid of the complete inner loop and a variable in the process. The less number of variables that happier I am :D

avatar image SpaceManDan · Mar 26, 2018 at 04:15 AM 0
Share

What a completely thorough and simple answer. @Bunny83 I stumbled on this looking for a different solution to an issue I'm having when reporting from a static class, static coroutine and gave this a read. Couldn't have said it better myself upvote +1

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What am I doing wrong with IEnumerator? 1 Answer

GameObjects static array NullReferenceException 1 Answer

Where is the Mistake in my Coin System? 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