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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by infamybrian · Jul 31, 2013 at 08:25 PM · inspectorgetcomponentinheritance

Why Doesn't Getcomponent add script to game objects inspector automaticly?

Hello guys, right now I'm playing around with unity and have read up alot on GetComponent. I have a Script that inherits from another custom script. The problem is using .getcomponent doesn't add the needed script to the game object automaticly. So I find my self having to use to add it manual. Is there just a normal limitation of unity with C#? While Reading Forums people using Java have no problem using .GetComponent<>

 using UnityEngine;
 using System.Collections;
 
 public class TestAiPlayer : NormalAi
 {
     Country Liyzan;
     NormalAi LiyzanPlayerAi;
 
     // Use this for initialization
     void Start ()
     {
         Liyzan = GetComponent<Country>();//works just fine if Country.cs is placed manually
         LiyzanPlayerAi = GameObject.Find ("PlayerAI").GetComponent<NormalAi>();//works just fine if NormalAi.cs is placed manually
         Liyzan.Init("Liyzan",300000,200,true);
         print (string.Format("{0}",Liyzan.Name));
         //LiyzanPlayerAi.AiInit(Liyzan);
     /**********************************/    
         
     }
     
     // Update is called once per frame
     void Update ()
     {
     
     }
 }

Sure I can use .AddComponent(); but I just want to know if this is necessary or am I not using this method correctly. Thanks for the Help Guys

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 nixcs2512 · Jul 31, 2013 at 08:32 PM 0
Share

I dont know what "no problem" means. You mean if you call GetComponent

avatar image nixcs2512 · Jul 31, 2013 at 08:37 PM 0
Share

I dont know what wrong with my comment. Just typed it 5 6 times and missed so much characters. Anyway @jbarba_ballytech 's answer is what you need.

1 Reply

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

Answer by hoy_smallfry · Jul 31, 2013 at 08:30 PM

It doesn't add it because it is not meant to add it, only get it; hence the name. You can remedy the situation one of two ways:

1) If a script is dependent on another component (in this case, dependent on another script), you can use the RequireComponent attribute like so:

 [RequireComponent(typeof(Country))]
 public class TestAiPlayer : NormalAi
 { 
 ...
 }

This makes sure that when you add TestAiPlayer, it attaches Country to the GameObject right before it attaches TestAiPlayer.

2) You can also use the ?? operator, like so:

 Liyzan = GetComponent<Country>() ?? AddComponent<Country>();

This operator checks the value on the left side of the ??. If it is not null, it uses the value on the left side, and if it is null, it uses the right side instead.

The ?? operator is basically shorthand for this:

 Liyzan = GetComponent<Country>();
 if(Liyzan == null)
 {
     Liyzan = AddComponent<Country>();
 }
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 infamybrian · Aug 01, 2013 at 09:01 PM 0
Share

Jbarba, thank you so much for the advice. I never though of using a Attribute(I never use attributes, still hard headed about those because of starting with plain old C). I'll definitely start giving
attributes more thought in my code design. Thanks again.

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

18 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

Related Questions

Assign object in hierarchy as public variable in prefab 1 Answer

Inventory / inhertance driving me insane - help with theory please. 1 Answer

How do i NOT break encapsulation with Unity? 2 Answers

HideInInspector with inherited variables 1 Answer

Make a field in inspector hold inheriting classes 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