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
0
Question by Catlard · Aug 24, 2012 at 03:58 AM · reflectionyieldwaitforsecondstype

How Do I pass an unknown class to a function?

EDIT: SOLVED: Using delegates is way better than attempting to do what I was doing. See this video: http://www.youtube.com/watch?v=CtIX1ei1I9Y

Howdy!

I'm attempting to create a stopwatch class that will allow me to yield for real world seconds, as opposed to Time.timeScale seconds--this is a problem because I'll yield Waitforseconds in the game for 3 seconds, and then almost immediately for whatever reason the player might want to use a bullet-time function to slow down time. But the thing that was going to happen in three seconds will now no longer actually happen in 3 seconds--it will take hours in real time. So...long story short, I need to be able to pass some information to a yielding function that will then call a function from a string on the calling class. Most of that I've figured out--but there's a slight problem calling the function that I'm not understanding. Here are the two classes I'm attempting to use. The error is: error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

 public class Howdy : MonoBehaviour {
     
     public SomewhereElse _somewhereElse;
     
     void Start() {
         _somewhereElse.CallSayHi<this.GetType()>("SayHi");
     }
     
     public void SayHi() {
         print("Hi!");    
     }
 }


 public class SomewhereElse : MonoBehaviour {
     
     
     public void CallSayHi<T> (string functionName) {
         MethodInfo mInfo = typeof(T).GetMethod(functionName);
         mInfo.Invoke(T, null);
     }
     
 }

Thanks for your time...

Simon

Comment
Add comment · Show 7
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 Eric5h5 · Aug 24, 2012 at 04:27 AM 0
Share

Seems like it would be a lot easier just to make your own WaitForSeconds coroutine that takes timeScale into account.

avatar image Catlard · Aug 24, 2012 at 04:37 AM 0
Share

Yes, that's the goal, and I had been just multiplying the waitforseconds value by the timescale amount to get the right time. But this only works if the WaitForSeconds is meant to start and stop within the newly adjusted timescale. If I have an always changing timescale, this requires something a bit more precise...non?

Thanks for the quick response, Eric!

avatar image Eric5h5 · Aug 24, 2012 at 04:41 AM 0
Share

Yes, as I said, make your own WaitForSeconds function, and use that ins$$anonymous$$d of WaitForSeconds...make a loop in the function that either takes timeScale into account every iteration, or uses Time.realtimeSinceStartup.

avatar image Catlard · Aug 24, 2012 at 04:46 AM 0
Share

Ok, yup! Cool, that part I have indeed figured out already...that's no problem at all. What I'd like, though, and I think what this question is really asking, is how to cause that custom waitforseconds function to call a function from any calling class based on parameters, when it's finished yielding. Something that works similarly to the "oncomplete" hash table variable in iTween, if you catch my drift?

Thanks again!

avatar image Catlard · Aug 24, 2012 at 05:06 AM 0
Share

Okay, I'll have a look into delegates, good idea. Do you prefer to use delegates over reflection in this situation, though? Victorino seems to be proposing a solution that uses reflection.

Show more comments

2 Replies

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

Answer by Eric5h5 · Aug 24, 2012 at 04:51 AM

Do you need that? If the goal is just to wait real-world seconds, all you have to do is yield on that custom coroutine the same way you would do WaitForSeconds. If you need to do other things when the coroutine is finished for some other unspecified reasons, then I'd recommend looking into delegates.

Comment
Add comment · 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
1

Answer by lvictorino · Aug 24, 2012 at 04:50 AM

Generics are made for that. It allows you to use an undetermine type in a methode or a class. This is how GameObject.GetComponent method works actually.

Try this:

 void CallSomethingElse <T>(string functionName) 
 {
    // then do whatever you want to do using T instead of callingclass
 }

then you can call it this way:

 myCallSomethingElseInstance.CallSomethingElse<my_undetermined_class>( my_function_name );

You can read more about generics here : http://msdn.microsoft.com/library/512aeb7t(v=vs.80).aspx

Hope it helps.

Comment
Add comment · Show 10 · 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 Catlard · Aug 24, 2012 at 05:03 AM 0
Share

Ah! I think this is what I'm interested in. Now I think I just have a syntax problem. I'll add it to the question.

avatar image lvictorino · Aug 24, 2012 at 05:24 AM 0
Share

Now that I see what you want to do... I think the solution is what Eric5h5 is proposing. Start a coroutine then raise a deleguate once its finished. I guess @Eric5h5 deserves the "right answer".

avatar image Catlard · Aug 24, 2012 at 05:35 AM 0
Share

Wait, so...it's impossible to send an undeter$$anonymous$$ed class reference to a function? The above code will never work?

avatar image lvictorino · Aug 24, 2012 at 05:39 AM 0
Share

Of course it will, but for what you intend to do this is not the best solution imho. I'll post you (later) the solution for the code above if you want. But in the same time I suggest you have a look at delegates :).

avatar image Catlard · Aug 24, 2012 at 05:42 AM 0
Share

Ok, I will have a look at some of these fancy delegate things you keep mentioning. I would be very interested to know how your previous solution might work, though. Cheers!

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

11 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

Related Questions

How to delay a function (how to use WaitForSeconds() and yield) 1 Answer

WaitForSeconds problem with Unity Pro 3 Answers

Wait For Seconds to Load level C# 2 Answers

Yield refuses to work. Js 2 Answers

waitforseconds not working 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