- Home /
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!
How is PlayerDataClass declared? It cant say $$anonymous$$onoBehaviour in it.
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;
}
}
As with GetComponent, it's rarely a good idea to use strings in AddComponent.
playerScripts[i] = players[i].AddComponent(PlayerScript);
@Eric5H5 Hehe, seems like not a day goes by without you having to explain the difference between the various GetComponent() :p
@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...
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.
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
Follow this Question
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