Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by thenelson · Sep 30, 2011 at 05:24 AM · instantiateprefabclass

Instantiate Prefab from within class

I'm having a bit of difficulty figuring this out, but I think I am overlooking something simple.

I had my script for a level generator which would instantiate prefabs to build the level. It followed this pattern, just in the root of the .js file which I attached to a blank object in the scene.

 public var wall : Transform;
 
 function Start () {
     for (var x = 0; x < 10; x++){
         var newTile : Transform;
         newTile = wall;
         var instTile : Transform = Instantiate(newTile, Vector3(x, 0,0),Quaternion.identity);
     }
 }

At which point I drag the prefab onto the script variables area in the Inspector.

However, I want to create a class that will do the instantiating. I know that I need to use GameObject.Instantiate, but my issue comes from not having the Prefabs available within the class.

If I create a class and try to access the prefabs, it says it cannot recognize the prefabs.

Here is an example of what I was trying using classes:

 public var wall : Transform;
 
     class area{
         
         function area(){
             var newTile : Transform;
                 newTile = wall;
                 var instTile : Transform = GameObject.Instantiate(newTile, Vector3(x, 0,0),Quaternion.identity);
         }
     
     }
     
     function start(){
     
         var currentZone = new area();
     
     }

Which tells me that there is no such object as "wall". If I declare the prefabs within the class, I can no longer drag them onto the script in the Inspector Panel.

Am I missing something obvious here?

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 dai1741 · Sep 30, 2011 at 08:28 AM

As far as I understand, an inner class is not able to access the outer class' variables implicitly in UnityScript. You can access them by supplying the outer instance explicitly, it's not so elegant though.

Here's a fixed script:

 public var wall : Transform;
 
 class area{
     var outer;
     function area(_outer) {
         outer = _outer;
         var newTile : Transform;
         newTile = outer.wall;
         var instTile : Transform = GameObject.Instantiate(newTile, Vector3(0, 0,0),Quaternion.identity);
     }

 }

 function Start(){
 
     var currentZone = new area(this);
     
 }
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 thenelson · Sep 30, 2011 at 03:08 PM 0
Share

Thanks, that does work. I hadn't known you could pass the outer class like that!

avatar image
1

Answer by thor_tillas · Sep 30, 2011 at 08:16 AM

There is two ways to spawn some prefab on the fly.

First one : Use a list of public members which describe each prefab you need for your level and spawn them in the start or awake function. For example (sorry, but I can't write js script... I hope you are able to translate C#):

 public class LevelLoader : MonoBehavior
 {
   public Transform WallPrefab;
   public Transform EnnemyPrefab;

   protected void Start()
   {
     GameObject wallObject1 = GameObject.Instanciate(this.WallPrefab);
     GameObject wallObject2 = GameObject.Instanciate(this.WallPrefab);
   
     // ...
   }
 }

Or you can use the Resources.Load() method. Put your wall prefab in the Resources folder and then use the load function to instanciate your prefab.

Hope it's helped...

cheers, Thor

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to create multiple Characters? 2 Answers

Get unity to recognize prefab in C# 2 Answers

Instantiated object rotation abruptly. 0 Answers

Instantiate prefab (button, texture) with attached script 1 Answer

Can I call a class's method which inherit monobehaviour by a normal class? 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