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 gumboots · Apr 09, 2012 at 08:58 AM · javascriptmonobehaviourclasses

JavaScript Classes

Hi there,

I've seen a few posts about using Javascript files as classes and fixing the warning:

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

However I'm not sure how to apply the solution to my game. I have a Javascript class as follows:

 public var networkPlayer : int;
 public var playerName : String;
 public var playerScore : int;
 public var playerTeam : String;
 
 public function PlayerDataClass() : PlayerDataClass
 {
     var capture : PlayerDataClass = new PlayerDataClass();
     capture.networkPlayer = networkPlayer;
     capture.playerName = playerName;
     capture.playerScore = playerScore;
     capture.playerTeam = playerTeam;
     
     return capture;
 }

So in a separate script (PlayerDatabase.js) I declare at the top:

 public var PlayerList : List.<PlayerDataClass> = new List.<PlayerDataClass>();

And then:

 function AddPlayerToList(nPlayer : NetworkPlayer)
 {
     var capture : PlayerDataClass = new PlayerDataClass();
     capture.networkPlayer = int.Parse(nPlayer.ToString());
     PlayerList.Add(capture);
 }

So I'm not sure if AddComponent() will work how I need it to? Any help would be incredible!

Comment
Add comment · Show 1
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 JimmyJJeeter · Apr 09, 2012 at 06:10 PM 0
Share

How is PlayerDataClass declared? It cant say $$anonymous$$onoBehaviour in it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gregzo · Apr 09, 2012 at 02:36 PM

MonoBehaviour scripts need to be added to GameObjects. General example, useful to keep a reference of the scripts your adding:

 var players : GameObject[];
 var playerScripts = PlayerScript[];

 function Start()
 {
      playerScripts = new PlayerScript[players.length];

      for(var i:int=0;i<players.length;i++)
      {
           playerScripts[i] = players[i].AddComponent("PlayerScript") as PlayerScript;
      }
 }
Comment
Add comment · Show 5 · 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 Eric5h5 · Apr 09, 2012 at 06:27 PM 1
Share

As with GetComponent, it's rarely a good idea to use strings in AddComponent.

 playerScripts[i] = players[i].AddComponent(PlayerScript);
avatar image by0log1c · Apr 09, 2012 at 07:54 PM 0
Share

@Eric5H5 Hehe, seems like not a day goes by without you having to explain the difference between the various GetComponent() :p

avatar image gregzo · Apr 09, 2012 at 10:06 PM 0
Share

@Eric5h5 Thanks, I didn't know that. Just found a more developped reply of yours about it, very informative. It's a pity the docs give the quoted version as an example...

avatar image Eric5h5 · Apr 09, 2012 at 10:40 PM 0
Share

The docs provide examples for all variants, so indeed they have an example using a string, as they should, as well as an example for the non-string version. It would be good, though, if they discouraged the usage of the string version, although to be fair, they do in fact do that for GetComponent.

avatar image gumboots · Apr 10, 2012 at 01:13 PM 0
Share

Thanks guys, but I still don't understand the concept behind it. I get that if you extend $$anonymous$$onoBehaviour it has to be attached to a GameObject. But I'm not sure how to apply that to my code. Whenever the PlayerDataClass is called it creates a new list of objects that extends from that.

So I AddComponent the PlayerDataClass the first time, however subsequent players can't add a PlayerDataClass to the PlayerDatabase every time, can they? This is where I get confused.

Is there a way I can not extend $$anonymous$$onoBehaviour? I've tried adding:

class PlayerDataClass { }

Around my code, but then it complains it can't return anything, and if I add : PlayerDataClass after it it definitely doesn't like that either.

All help, again, appreciated.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use Javascript Array with classes? 1 Answer

How to get a reference to the owner-Script from inside a Class which is being used as field/property 1 Answer

Custom JS class error - function is not a member of the class 2 Answers

Referencing the object that a construtor is currently constructing/working on. 1 Answer

Creating a Loadout 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