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 Bluestrike · Nov 21, 2014 at 03:32 PM · javascriptlistmonobehaviour

Call the same function on different scripts.

I am trying to call the same function on different scripts. I place all scripts into an array and then try to call that function by iterating trough all scripts.

  private var SavableScripts : List.<MonoBehaviour> = new List.<MonoBehaviour>();
  
  public function SaveNewGame()  : void
  {
 5.     for ( var i : MonoBehaviour in SavableScripts );
      {
          i.OnSaveGame();
      }
  
 10.     // 2do hide saving icon
  }
 

This does not seem to work, it can't find that member function OnSaveGame().

How can I accomplish this, do I have to look into generic functions using c# instead of this unityscript attempt of mine?

I do not wish to call BroadcastMessage and the like because I will not know when saving is completed that way.

Comment
Add comment · Show 4
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 richyrich · Nov 21, 2014 at 04:07 PM 0
Share

C# Would be a lot easier to use. You would create a base class that implemented the OnSaveGame method as virtual. Each derived class would also use the virtual keyword prefix to OnSaveGame. You could then create your list from your new base class.

In javascript, this is the closest workaround I could find:

[link removed as it was JavaScript, not Unityscript/javascript]

avatar image Bunny83 · Nov 21, 2014 at 04:15 PM 0
Share

@richyrich: What you've linked is EC$$anonymous$$A Javascript and not UnityScript. Unity doesn't support Javascript at all. UnityScript is a completely different language which they unfortunately called Javascript. UnityScript is syntactically similar to Javascript but that's all.

avatar image richyrich · Nov 21, 2014 at 04:31 PM 0
Share

@Bunny83: Thanks. It's all very confusing that something called JavaScript in the Unity program itself, is syntactically similar to JavaScript but not actually supported javaScript. Think I'll stop trying to help with the javascript and stick to c#, which is presumably still c#, not unity c#!

avatar image Bunny83 · Nov 21, 2014 at 04:44 PM 0
Share

@richyrich: Right ;) UnityScript / Javascript is supposed to make it easier for beginners, however the way it works is completely different from the normal "Javascript". So if a webdeveloper starts using Untiy he will have a lot of "wtf" moments as almost nothing he knows applies to that "Javascript" in Unity.

C# is basically C#. Though since it's $$anonymous$$ono and not .NET the version of C# can't be deter$$anonymous$$ed in relation to the $$anonymous$$SDN docs. Some things might not be implemented (yet). However everything that is supported works exactly the same as in .NET.

1 Reply

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

Answer by Bunny83 · Nov 21, 2014 at 04:27 PM

You basically have 4 options:

  • Use Invoke on that MonoBehaviour reference.

  • Use a base class with a virtual method that gets overridden in your derived classes

  • Use an interface (that has to be defined in C# as that's not possible in UnityScript afaik) and let your classes implement that interface.

  • Use reflection to sneak into the acual class, find the method you want to call and invoke it manually.

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 Bluestrike · Nov 22, 2014 at 08:19 AM 0
Share

Invoke does not wait for the invoked function to complete so I'll have a shot at your 2nd suggestion.

I think its this?

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/polymorphism Also UnityScript seems to have limited interface support.

In both cases I will have to make my scripts classes so I can extend them. So I better get started :)

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

28 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

Related Questions

A node in a childnode? 1 Answer

Attached scripts as List? 1 Answer

What can i do? Unity no responding when i load a list. 1 Answer

How can I set the properties of serialised fields on an object recusively? 0 Answers

Get variable from Script in list and , set direct link to other script 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