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 prateems · Feb 28, 2018 at 04:42 PM · scripting problemgetcomponentprefabsinheritanceproperties

When or where do I set a prefab script subclass property?

I have a GargoylePrefab in Unity that is assigned to a list of objects in my component Units. The prefab has a Gargoyle script attached to it (which inherits from the abstract Enemy).
.
I am having trouble setting the inherited property EnemyType Type in my GargoylePrefab object according to the script. When I try accessing the script component of the prefab and logging its Type property, I get an unexpected/unwanted result ( Invalid instead of Gargoyle).
.
Am I setting the value in the wrong place? I have also tried setting it in Gargoyle.Awake() but the result is the same. How do I fix this problem?
.
See below for implementation overview:

 // EnemyType.cs
 public enum EnemyType { Invalid, Gargoyle }
  
 // Enemy.cs
 public abstract class Enemy : MonoBehaviour {
     // Because EnemyType's first definition is "Invalid"
     // Subclasses of Enemy have to set their type to something else
     // to avoid having an unwanted or undefined type
     public EnemyType Type { get; protected set }
     ...
 }  
  
 // Gargoyle.cs
 public class Gargoyle : Enemy {
     private void Start() {
         Type = EnemyType.Gargoyle;
     }
 }  
  
 // Units.cs
 public class Units : MonoBehaviour {
     public GameObject[] EnemyPrefabs;

     public void OutputPrefabTypes() {
         // This is just a method for testing.
         // I -know- only one GameObject (GargoylePrefab) is in the array.
         foreach (GameObject prefab in EnemyPrefabs) {
             Enemy genericRef = prefab.GetComponent<Enemy>();
             Gargoyle gargoyleRef = prefab.GetComponent<Gargoyle>();

             Debug.Log(genericRef.Type);  // Expected: Gargoyle, Actual: Invalid
             Debug.Log(gargoyleRef.Type); // Expected: Gargoyle, Actual: Invalid
         }
     }
 }
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
0
Best Answer

Answer by prateems · Feb 28, 2018 at 07:22 PM

The lack of instantiation of the prefabs stored in EnemyPrefabs is the issue, as Start() and Awake() lifecycle methods are only called on instantiated instances of the prefab.
.
The solution is a design change:

 public abstract class Enemy : MonoBehaviour {
     public abstract EnemyType GetEnemyType();
     ...
 }  

 public class Gargoyle : Enemy {
     public override EnemyType GetEnemyType() {
         return EnemyType.Gargoyle;
     }
 }  

And then calling GetComponent<Enemy>().GetEnemyType(). Doing this provides the desired results.

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

Answer by JedBeryll · Feb 28, 2018 at 05:33 PM

Possible cause: OutputPrefabTypes is called before Gargoyle.Start Thus it has the default value of EnemyType. Fixes: either set Gargoyle's execution order to a smaller value so it's called sooner (could get messy if you have many scripts) or make sure Gargoyle is initialized before calling any methods on it.

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 prateems · Feb 28, 2018 at 07:22 PM 0
Share

Your comment led me to realize that instantiation was indeed the problem. Because the Start() and Awake() code only runs on instantiation, and only for the instantiated instance of the game object, I would never get the desired output with the way I set it up.

I added my revised solution as an answer.

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

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

Related Questions

Is it possible to set variables for classes? 2 Answers

SteamVR - Spawning Prefab on Controller,SteamVR - Spawning Objects on Controller 0 Answers

Exit Prefab Mode from custom editor 0 Answers

C# Parent-SubClass set inherited properties help 1 Answer

I need to make two bullet controllers (friendly and enemy) Should I inherit? 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