• 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 Masonface · Oct 01, 2012 at 01:19 AM · gameobjectinstantiatelist

Instantiate directly from Project/Prefabs

Hi!

I'm a beginner programmer making a vertical shmup game and I'm trying to create an efficient method for instantiating enemies into the scene. I'm working on extending the editor to make the inspector more versatile for what I'm doing. Ideally, I'd like to have drop down lists for the user to be able to select the enemy type, weapon type, etc., by using enumerators and having the script search for the prefab that corresponds to that selection from the project/prefabs folder.

I understand that the most common and practical way of instantiating a prefab is to initialize a GameObject variable and then drop the prefab into that slot in the inspector, but I really think that a drop down list would look more polished and be a little easier of a workstream for the level designers.

Okay, so the approach that I've tried to get this to work is admittedly very crude. I've tried to create an empty GameObject called "ObjectLibrary" with an attached script that stores all the prefabs, called "ObjectLibrary.js." My line of reasoning was that I could let the script attached to the enemy search for the GameObject it needs from the ObjectLibrary. However, when I try to retrieve the needed GameObject, Unity returns ". . . 'EnemySmall' is not a member of 'UnityEngine.GameObject'. "

If you see anything wrong with my code, please let me know. Otherwise, if this approach is absurd, please enlighten me on a better method to get the desired results! Either way, thank you so much for your help!

Here is the code attached to the enemy:

 #pragma strict
 
 enum Type {Small, Big}
 enum Weapon {Yellow, Red}
 
 var EnemyType : Type;
 var WeaponType : Weapon;
 var Speed : float;
 var Enemy_Ship_Health : float = 100.0;
 var ScoreValue : int = 100;
 var SpawnTime : int = 2;
 
 
 private var ObjectLibrary : GameObject;
 private var EnemyShip : GameObject;
 private var Clone : GameObject;
 
 function Start () {
 
     ObjectLibrary = GameObject.Find("ObjectLibrary");
 
         if(EnemyType == Type.Small){
             
             EnemyShip = ObjectLibrary.EnemySmall;

//"If the user selected a small ship type, then look to 'ObjectLibrary' to see what GameObject is defined as 'EnemySmall'

             yield WaitForSeconds(SpawnTime);
             
             Clone = Instantiate(EnemyShip, transform.position, transform.rotation);
             Clone.transform.parent = gameObject.transform;
         
                 
     }
 
 }

And here is the code attached to the GameObject called "ObjectLibrary" (just simply variable declarations for the GameObjects that I'm trying to reference):

 #pragma strict
 
 
 var EnemySmall : GameObject;
 var EnemyBig : GameObject;
 var RedProjectile : GameObject;
 var YellowProjectile : GameObject;
 

Again, any help would be very much 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Oct 01, 2012 at 10:08 AM

Your ObjectLibrary is of the type gameObject and is not your script. Use

 var script = ObjectLibrary.GetComponent(ScriptName);

to get your script, then use

  script.EnemySmall
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 Masonface · Oct 01, 2012 at 01:55 PM 0
Share

It works perfectly! Thank you very much!

avatar image $$anonymous$$ · Oct 01, 2012 at 02:55 PM 0
Share

Your welcome :)

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

10 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

Related Questions

Add force to each object individually from a list immediately after it has been instantiated. ,Applying force to a listed object after it has been instantiated 1 Answer

What is the best way to instatiate an array of connected GameObjects? 0 Answers

Store Game Object Into List For Later Reinstantiation? 0 Answers

Looping through multiple lists 0 Answers

Cant instantiate from one list to another? 2 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