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
1
Question by The_r0nin · Jun 08, 2010 at 07:53 PM · javascriptinstantiateinheritanceclasses

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!

Comment
Add comment
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

2 Replies

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

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;
Comment
Add comment · Show 2 · 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 The_r0nin · Jun 08, 2010 at 08:12 PM 0
Share

$$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!

avatar image The_r0nin · Jun 08, 2010 at 08:29 PM 0
Share

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...

avatar image
0
Best Answer

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

Comment
Add comment · Show 3 · 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 The_r0nin · Jun 08, 2010 at 08:15 PM 0
Share

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?

avatar image qJake · Jun 08, 2010 at 08:32 PM 1
Share

Use AddComponent(); to add script components to game objects during runtime. - http://download.unity3d.com/support/documentation/ScriptReference/GameObject.AddComponent.html

avatar image The_r0nin · Jun 08, 2010 at 09:17 PM 0
Share

Thanks! I guess I'll try the prefab + script method suggested here...

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

No one has followed this question yet.

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


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