Built In Type for Script?

Hi everyone. Im curious if there is a built in variable type representing a script. Just like String , int, GameObject etc… is there a way to create a script variable. For eg :

var LogicScript : … ;

Might sem like a silly question , but i tried component , it doesnt seem to work, and i cant seem to find anything around googleing either . Thanks in adavance

The type is the name of the script.

–Eric

uhhhh , thats kinda a problem… im trying to have it as a global variable in a different script. Is this even possible?

var PlayerCharacters : PlayerCharacter[];

class PlayerCharacter{
	var Character : GameObject;
	var CharacterLogic : Component;
}

function OnGUI()
{
	GUI.Box(Rect(0, 0, Screen.width, Screen.height), "");
	var totalPlayers : int = 0;
	for(var playerCharacter in PlayerCharacters)
	{
		totalPlayers++;
	}
}

Basiclly what i want to do , is have a model be pluggable. and a script for its logic be pluggable …

and i want this object to support several of them being plugged in at one time, thus the array

To expand on Eric’s reply a bit :slight_smile:

So, if you have a script called Player.js… you could declare in another script…

var mainPlayer : Player;

Then you could either drag a GameObject with the Player script onto it, of you can assign it at runtime.

You cant do that because you are declaring PlayerCharacter as a class. To use the class way, you would do this…

Then…

for each character you were creating.

Nah , i understand what your sayin i think , but thats not what i want to do (if im understanding correctly). I want to be able to change the script through the global variable with ease. So for eg: i have a warrior, rogue, and a Monk or something … ((these are all models animated etc ) so i drag them into the three gameObject vars, and than i attach a logic script to each of them., but i want to be able to change these on on the fly during testing and development … like maybe give the warrior the monks logic. or whatever. I just wondered if there was a way to have a global variable ((in the inspector)) that accepts scripts as the value.

ooo you wrote before me, i see im making a mistake in how i use them. Thanks , i will try some more :stuck_out_tongue:

Ahh i was way overthinking this one. Took me a half hour to realise how redundant this procedure would be based on prefabs … Thanks again.

No, that part was correct. I discourage anyone from using Array; it’s slow and obsolete.

Anyway, I don’t know what your setup is, but a simple (though not ideal) way is to use strings:

var playerCharacters : PlayerCharacter[]; // use lowercase for variable names; it's less confusing

class PlayerCharacter {
	var character : GameObject;
	var characterLogic : String;
}

That’s not as fast as using the type directly, and there’s no error checking except at run-time, but it does work when used in GetComponent and AddComponent.

As for this:

function OnGUI()
{
	GUI.Box(Rect(0, 0, Screen.width, Screen.height), "");
	var totalPlayers : int = 0;
	for(var playerCharacter in PlayerCharacters)
	{
		totalPlayers++;
	}
}

Definitely don’t do that; OnGUI runs more than once per frame. Obviously you can leave the GUI.Box part, but take the rest out and put it somewhere that runs only when necessary.

–Eric

Hmm i tried it that way for awhile, and it kept coming back that my object was destroyed and something else… i would have to resimulate it now to make sure . But either way i have not got it working yet. I was thinking to myself though , that it seemed silly to even go the route i was going, because really a warrior would have a warrior script attached to them as a prefab in general, and even if i wanted to swap the script out fast , its as easy as changing it in the editor. Just way over thinking things atm i think.

Ultimately , the question at hand was if you could declare a built in type of sorts that was visible in the editor. I did try this …

var myScript : MyScript;

and it was visible in the editor, but appeared as if nothing could be assigned to it at all. Additionally, none of the other scripst code seemed to do anything at runtime.

But Unity inbult Arrays don’t allow for normal Array methods like adding, popping, shifting, sorting etc… so how do you dynamically add new characters to the built in array at runtime without re-initialising the array with a new size, killing everything in it?

If you need a dynamically-sized array, use List.

–Eric

Hey Eric,

I have not looked at Lists…Until now. So how is doing this…

any different from doing this…

Are Lists a better, newer type of Array?

I found some .Net reference for List<>, and I am assuming in JavaScript (UnityScript) I declare it like I am above, or can I do something like… btw, this doesn’t compile for me, but maybe I have the syntax wrong.

Cheers :slight_smile:

http://unity3d.com/support/documentation/Manual/MonoUpgradeDetails.html

var playerCharacters = new List.<PlayerCharacter>();

If you knew you were going to have at least 100, you can do

var playerCharacters = new List.<PlayerCharacter>(100);

and it’s a little more efficient. Primarily, Lists are faster and type-safe. Generic classes and methods - C# | Microsoft Learn (ArrayList is pretty much equivalent to Array.)

–Eric

Wow, missed that! That’s very very cool. Made my night! Thanks Eric :slight_smile:

By the way, you should use

import System.Collections.Generic;

when using Lists. They work to some extent without it, but various functions need that namespace (and you don’t want to be putting System.Collections.Generic everywhere in your code of course).

–Eric

And i would like to thank you both for enlightening me on a few things :smile: