• 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 eaj · Mar 17, 2013 at 09:35 AM · instantiateprefabsclasses

Prefabs, Classes and GameObjects. I don't understand.

Hello,

I'm still trying to get my head around Unity's approach to OOP. Seems easy but I'm very confused with the notion of instantiating prefabs when you bring classes into the mix.

Here's my example.

I have a prefab of a planet and its pivot. The Planet gameObject has a meshFilter as a component which is just a sphere as well as a meshRenderer.

alt text

I've created a script called Planet and added this as a component to the Planet GameObject. Here's the code.

 #pragma strict
 
 public class Planet extends MonoBehaviour { 
     
     var planetName : String;//name
     var planetObject : GameObject;
     
     function Planet(name : String){
         planetName = name;
         planetObject : gameObject
     }
     
     function Start () {
     }
     
     function Update () {
     }
 }

To run things I've got an empty GameObject called Main and I've added a script component to it. Here's the code.

 #pragma strict
 
 public var planet : Planet;
 
 function Start () {
     planet   = new Planet("Earth");
 }
 
 function Update () {
 }

What I'm trying to do is create an instance of the Planet prefab (and the planetPivot) but I've completely confused myself. I know I should be using instantiate but how does that work with classes? Should I be using classes at all? I want to use a class for inheritence.

Thanks in advance!

screen shot 2013-03-17 at 7.38.07 pm.png (7.0 kB)
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 Fattie · Mar 17, 2013 at 09:43 AM

just FYI. if you want, just don't use prefabs. I find them annoying and don't use them often.

 var newGameObject:GameObject;
 newGameObject = Instantiate( your model here );

just FWIW some discussion about making a pool , it has many Instantiate examples

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

Comment
Add comment · Show 1 · 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 eaj · Mar 17, 2013 at 11:53 AM 0
Share

Wow, that's a fantastic link and just what I needed! thanks.

I managed to get the Instantiation working but I did away with the planet class. I'll have a look at your solution next

Here's my solution.

I removed all the code from the Planet script. Essentially, for the time being, it simply holds a single variable called planetName.

And here's my main script

 #pragma strict
 
 function Start () {
     var pivot : GameObject;
     var planet : Transform;
     
     pivot  = Instantiate(Resources.Load("pivotPlanet"));
     planet = pivot.GetComponentsInChildren(Transform)[1];
     
     planet.position.y = 2;
 }
 
 function Update () {
 }

This loads the pivotPlanet asset from the Resources folder and instantiates it. From there I get the second trasform child, which is the planets transform itself. Finally as a test, I translated the planet upward in Y by 2 units.

For those with the same problem: I didn't have a Resources folder in my asset hierarchy. Resources.Load() looks in this folder for any given asset so make sure this folder exists and your assets are in there.

avatar image
2

Answer by robotunity · Feb 01, 2015 at 08:29 AM

I'm still trying to get my head around Unity's approach to OOP.

This is because most Unity documentation completely ignores it. The notion of manually dragging simple things into a "scene" is heavily favored for some reason, instead of how to program a functional game.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

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

Related Questions

Instantiate Reference Problem 1 Answer

Prefabs wont instantiate using a for loop? 1 Answer

instantiate prefabs/gameObjects 1 Answer

How to instantiate the prefabs on grid in Unity? 1 Answer

Spawning letter tiles evenly across screen 0 Answers


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