Is it possible to make custom automatically-called methods like Start and Update?

Updated on October 11, 2016 in [A] Unity Scripting
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
6 on October 11, 2016

I didn’t get answers on Unity Answers, and I don’t want to write it all again, so I will put a link to the question on Unity Answers here: http://answers.unity3d.com/questions/1253520/is-it-possible-to-make-custom-automatically-called.html

I am actually pretty ashamed of myself for just posting it here…it probably didn’t get answers for a reason like the post not being detailed enough, or something else. Though on the other hand…I get here more answers with the same post, so who knows.

Edit/update:
Someone asked me to be more specific…I actually don’t see how it could help much, and I do see how it will make the post longer, but if you need extra details, read it.
I have in my game an event that starts/ends a battle. The interface I made that derives from IEventSystemHandler is called “IFighter”, and every object that can get into a fight will implement it. One of the methods this interface has is called “OnBattleEvent”, and it takes a parameter for which event, start or end? I used an enum to do this. It’s because it looked pretty inefficient when I had an OnBattleStart event and OnBattleEnd event. On a completely different script, that is not related at all (except…it is, but it doesn’t implement the interface), I have a method called StartBattle and a method called EndBattle (in that case it makes sense to have them separated). They do things you need to do to actually start a battle, and it also invokes the OnBattleEvent event. The problem is that I don’t know how to do it, since the ExecuteEvent.Event method asks for a target.

I am pretty sure I said it in the original post as well, but in case i didn’t, I will say it again: The reason I don’t use a C# event, or a Unity Event, is because when I use those I must remember to add and remove the method, which is both risky and annoying. So using those automatically called methods is the best.

Of course I could easily do it using an abstract class, but the problem with that is that some of them will have different parent classes. Interfaces are just the best way.

  • Liked by
Reply
1 on October 11, 2016

Reading over the original post, I realized that I was still quite confused with what you were looking for. I think it would be beneficial to explain specifics. What method specifically are you trying to create, and in which circumstances would you like to call on it. From there, you might be able to then extrapolate the process and put it into a universal perspective which can then be applied universally to any future, generic method/function. 
 
So, I suggest you ask for specific answers to a narrow-scoped problem. then see how you can generalize it afterwards.

Devoted
on October 11, 2016

I updated my post, hopefully it’s better now.

Show more replies
  • Liked by
Reply
Cancel
3 on October 11, 2016

Honestly, from the way I’m reading…
Events really are the way to go. Don’t know if you’ve ever worked with UI in normal C#, but they use events for everything.

Devoted
on October 11, 2016

You mean C# events? Well, that’s going to be pretty annoying… Well, time to add an OnEnabled, OnDisabled AND OnDestroy to my scripts that use it… Well, let’s just hope making methods like Start and Update will come in future versions of Unity, it will be awesome. 😉

Guru
on October 11, 2016

What do you mean? Why would you want to make a OnDestroy event? You can just use the methods that Unity gives you right?

Devoted
on October 11, 2016

No, I meant create the method. It will just take up more place, and will take much more effort to simply add and remove actions from an event.

Show more replies
  • Liked by
Reply
Cancel