- Home /
Difficulty instantiating to a custom class
I am trying to extend the GameObject class to allow me to add functions/methods that I can use without the overhead and complexity of static functions. I'm trying to instantiate a obj file from resources to an object of the new class (because, since it should inherit all of the members of GameObject, it should be able to do so, correct?), but I can't get the code to work (Javascript). A brief synopsis of my code would be as follows:
class ExtendedObject extends GameObject { public var stats: int
function ManipulateObject(inputValue:int){ stats = inputValue; } }
var newObject: ExtendedObject = Instantiate(Resources.Load("MyModel"),position,rotation) as ExtendedObject; newObject.tag = "Player"; newObject.ManipulateObject(6);
I know I'm missing something in my understanding here... but I'm not sure why I can't load a resource to the object when it should have inherited all of the features of a GameObject. Any help would be appreciated!
Answer by qJake · Jun 08, 2010 at 08:01 PM
The only problem I see with your script (potentially) is the Resources.Load() portion. What is "MyModel"? You can't instantiate models, you can only instantiate game objects, components, and prefabs (which are also game objects, inherently). If you want to be able to instantiate a model, insert it into an empty game object (by dragging it onto the scene), then create a prefab, and reference the prefab in your script. You also shouldn't use Resources.Load unless absolutely necessary, instead, create a public reference to a field in your script, and set its value using the Editor Inspector, like this:
public var myPrefab : GameObject;
Then, instead of instantiating using Resources.Load(), simply feed it the prefab, like so:
var newObject : ExtendedObject = Instantiate(myPrefab ,position, rotation) as ExtendedObject;
$$anonymous$$y mistake! "$$anonymous$$y$$anonymous$$odel" is an asset visible in the project pane. I used "Import New Asset" on an .fbx file and it created an asset. Unfortunately, I have dozens of such .fbx files, not all of which might be used during a particular play-through. It seems kind of wasteful to create a bunch of var references that might/might not get used (but I'm the amateur here, so I very well could be wrong...)
If I make an empty Object, drag my asset onto it, then drag it back into the project pane, will that give me a Loadable prefab that will work with my code?
Thanks for your help!
O$$anonymous$$. Your method does work. The only problem is that I get a "NullReferenceException" when it hits the line "newObject.tag = 'Player'." While I can see the new object in the game, it seems that the "tag" attribute is not being properly inherited or that the object is not fully being created. I may just have to move to creating a couple dozen prefabs with scripts attached and working that way. Sigh...
Answer by Tetrad · Jun 08, 2010 at 08:03 PM
You're not supposed to extend GameObject. If you need added functionality, you're supposed to make components that you attach to your game object.
See here: http://unity3d.com/support/documentation/Manual/Scripting.html
O$$anonymous$$. $$anonymous$$y issue then is that I am creating this object at runtime (based on a player's choices via the GUI). How do I attach a script/component to this new GameObject when it is instantiated (without creating dozens of duplicate prefabs... all with different models attached but with the same components?
Use AddComponent(); to add script components to game objects during runtime. - http://download.unity3d.com/support/documentation/ScriptReference/GameObject.AddComponent.html
Thanks! I guess I'll try the prefab + script method suggested here...
Your answer
Follow this Question
Related Questions
Defining and inheriting from a Javascript Class 0 Answers
How do I write this C# snipet in java script? 1 Answer
Using functions of Inherited classes 3 Answers
Creating a Loadout 1 Answer
Inheriting from classes from other files (Javascript) 1 Answer