• 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 Raux · Apr 26, 2014 at 04:04 PM · instantiateprefabenummethodproperty

Can't access enum property of an instantiated prefab.

Before you direct me to: http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html I could not find what I'm looking for.

And there are others that asked it too:

http://answers.unity3d.com/questions/21233/setting-properties-when-instantiating.html

http://answers.unity3d.com/questions/235943/instantiated-object-script-properties.html

But, I just don't understand it well enough due to my own lack of experience (just 2 weeks into C#) to solve my problem.


I decided that, instead of making a whole bunch of different prefabs, I just make one prefab that has a material array property and enum property.

I want to instantiate the prefab, then call a method on the instantiated gameobject to change the enum property and change it's material. I can't seem to figure out how to use GetComponent on the instantiated gameobject to do those two things.

Here is the code attached to the prefab I instantiate.

     public class PowerUpScript : MonoBehaviour {
     
         public enum EPowerUpType{Type1,Type2,Type3,Default}
 
         // powerUpType is set to Default in the unity inspector, it needs to be changed in the other class.
         public EPowerUpType powerUpType;
         
         // The inspector has 3 materials attached to this array.
         public Material[] powerUpMaterial;
     
         void SetPowerup(int i){
     //        powerUpType = EPowerUpType[i];
             GetComponent<Renderer>().material = powerUpMaterial[i];
         }
     }

This wont actually work, I commented out the powerUpType = EPowerUpType line because I have no idea how to specify it. I know enums store data as integers (Typ1=0, Typ2=1) but I can't seem to set EPowerUpType.i nor EPowerUpType[i].

Here is a snippet of the code that handles instantiating the prefab, which should also change the properties.

             switch(brickType){
             
             //...
 
             case EBrickType.Chest:
                 GameObject powerup = (GameObject)Instantiate(powerupPrefab, transform.position, transform.rotation);
                 PowerUpScript powerUpScript = powerup.GetComponent<PowerUpScript>();
                 powerUpScript.
                 break;
             }

But when I want to call powerUpScript.SetUpgrade(int); I just don't get the option to do so. Now I know that the Instantiate function returns a reference to what was just created, but apparently I'm doing something wrong here. It might be better to just change them to different prefabs, but I'm doing this to learn C# and unity so I might as well see how this way would work.

Comment
Add comment · Show 2
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 getyour411 · Apr 27, 2014 at 12:07 AM 0
Share

You don't show a method by that name in powerUpScript, if you mean SetPowerup you need to make it public void SetPowerup

avatar image Raux · Apr 27, 2014 at 11:38 AM 0
Share

Oh wow, I completely missed that!

1 Reply

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

Answer by NoseKills · Apr 27, 2014 at 12:04 AM

1) You should be able to just cast it

 powerUpType = (EPowerUpType)i;
 GetComponent<Renderer>().material = powerUpMaterial[i];

and reverse

 int intValue = (int)EPowerUpType.Type2;

2) You can't call void SetPowerup(int i) because it's not public. Make it public void SetPowerup(int i)

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access property of a prefab before Instantiating. 1 Answer

Instantiation in C# 1 Answer

prefab always instantiates at 0.0.0 1 Answer

Setting the parent of a transform which resides in a prefab error 1 Answer

prefab instantiated by script containing ShadowCaster2D bugs out 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