Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 shockelite · May 21, 2014 at 04:20 AM · c#instantiateprefabcomponent

Prefab with component, and attached sub component

My goal is to instantiate a prefab that contains 2 components that manage stats and behavior. The stat component will not change, but the behavior component will. Right now my behavior components contain a bunch of methods like onDrop, onAge, onDeathBySnuSnu (you get the point). And yes they are duplicate functions in every behavior component.

Is there a way for the stat component to only see behavior component within the prefab? I'm pretty new with c# so any guidance on the best practice would be 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
1
Best Answer

Answer by supernat · May 21, 2014 at 04:41 AM

  • for onDeathBySnuSnu!

To answer your question (if I understand it), yes, you can access the Behavior class instances on the prefab'd object (I mean the clone of the prefab in the game world, so if that's not what you are asking, sorry). It would be something like this:

 // In the Stat class, let's say in the Update method just for example:
 void Update() {
     MyBehaviorClass instance = GetComponent<MyBehaviorClass>();
     instance.onDeathBySnuSnu();
 }

As long as the MyBehaviorClass component is attached at the same level in the hierarchy on the game object as the Stat class component, it should be able to retrieve it using GetComponent. If it is higher up or lower down, you can instead say "gameObject.GetComponentInChildren()" I'm assuming there is only one MyBehaviorClass attached to each game object, but there are methods to return arrays of them if needed.

Comment
Add comment · Show 3 · 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 shockelite · May 21, 2014 at 05:53 AM 0
Share

Is there a way for the Stat class to detect which Behavior class is attached then assign to "instance"? Or is it possible to have multiple files have the same class?

--Edit-- I guess I'll just add a enum in the Stat class, then add if statements in the Behavior class. Thanks for your help supernat.

avatar image supernat · May 22, 2014 at 06:52 PM 0
Share

Okay, I guess I misunderstood. I thought you meant all of your behaviors were in the same class. Yes, you can check one of two ways. 1) using the "is" keyword or 2) checking for null after each test.

For instance, if you have a BaseBehaviorClass and all specialized classes derive from that, you can do something like this:

 BaseBehavior instance = GetComponent<BaseBehavior>();
 if (instance is SpecializedBehaviorSnuSnu)
     // Do whatever
 else if (instance is SpecializedBehaviorAge)
     // Do whatever else

In this case, it assumes you have only one behavior attached to your object still. If you had multiple behaviors, it would be more like this (though I may have some code compile issues here, it's the right idea):

 BaseBehavior[] behaviors = GetComponentsInChildren<BaseBehavior>();
 foreach (BaseBehavior behavior in behaviors) {
     if (behavior is SpecializedBehaviorSnuSnu)
         // Do whatever
     else if (behavior is SpecializedBehaviorAge)
         // Do whatever else
 }

The alternate approach is to check each behavior for null:

 SpecializedBehaviorSnuSnu instance = GetComponent<SpecializedBehaviorSnuSnu>();
 if (instance != null) {
     // Do whatever
 }

// repeat for other types

avatar image supernat · May 22, 2014 at 06:56 PM 0
Share

Note: I don't do the check for null option above very often, so I can't remember, but C# may throw an exception when you say GetComponent() if that component doesn't exist. In that case, just modify the test by placing a try/catch, and if you hit the catch block, you know the component doesn't exist.

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

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

Accessing component of a single prefabs' instance. 0 Answers

Spawning a prefab at another object's location 3 Answers

Issue Instantiating prefab in C# 0 Answers

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

Prefabs instantiated from an array are keeping their public int value 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