Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
3
Question by Feref2 · Jun 05, 2019 at 06:09 AM · getcomponentchecking

Is there a better way of checking if a gameObject has a component?

Am doing this:

if (previousObject.GetComponent() != null) { correctPos = previousObject.GetComponent(); }

But am getting the component twice. Is there a way to just check and have? Maybe is not that bad if I do this in awake or start, but it would be useful to know.

Comment
Add comment · Show 4
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 tormentoarmagedoom · Jun 07, 2019 at 08:23 AM 0
Share

Hello. Answer is not 100% correct. Unamrked until its modifyed.

avatar image SirPaddow tormentoarmagedoom · Jun 07, 2019 at 08:26 AM 0
Share

The first part of my answer remains valid. And I edited the second part to note that it was wrong...

avatar image tormentoarmagedoom SirPaddow · Jun 07, 2019 at 08:29 AM 0
Share

Yes, but you can not mark an aswer as correct if is not 100% correct.

I see there is a comment inside the code. I changed a little the format to make it clear that part is not working in Unity :D

avatar image Owen-Reynolds · Jun 13, 2019 at 04:41 AM 0
Share

This is just basic program$$anonymous$$g. If you want to use an equation a few times, compute it once and put it in a variable. Suppose x can't be more than y*2+7. Ins$$anonymous$$d of writing "if(x>y*2+7) x=y*2+7" you can compute it ahead of time: int x$$anonymous$$ax=y*2+7; if(x>x$$anonymous$$ax) x=x$$anonymous$$ax;

1 Reply

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

Answer by SirPaddow · Jun 05, 2019 at 06:21 AM

 Collider component = previousObject.GetComponent<Collider>();
 if (component != null)
 {
     correctPos = component;
 }

[EDITED] Removed incorrect part of the answer mentionning the "??" operator. see @Hellium comment below.

Comment
Add comment · Show 7 · 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 Feref2 · Jun 07, 2019 at 12:28 AM 0
Share

Thanks for the answer. Now am having a similar problem, and I want to know if the ?? also works. If int id != 0 { previousBaseId = id - 1;} So am doing this because the id needs to be at least 1 to get a 0 or a positive number as a result, else it should stay 0. If it´s 0 and subtracts, the id is wrong. I think this creates an error, but anyway, does the ?? work if is not posive and not just not null?

avatar image SirPaddow Feref2 · Jun 07, 2019 at 08:11 AM 0
Share

I'm not sure I understand your question, but in any case the "??" wont work with ints.

The operator ?? checks if the first value is null, but primitive types (such as int) are never null. If you try it, you will get a compiler error.

avatar image Hellium Feref2 · Jun 07, 2019 at 08:16 AM 0
Share

You will need the ternary operator to do what you want.

 int result = (condition) ? a : b;

which can be translated as follow:

 int result ;
 if( condition )
    result = a ;
 else
   result = b ;

In your case : int result = (id > 0) ? id - 1 : id ;

But more simply: if( id > 0 ) id--;

avatar image Hellium · Jun 07, 2019 at 07:29 AM 1
Share

The null-coalescing operator (`??`) does not work with Unity objects as indicated in this Unity blog post because it really checks against null. However, Unity does something special with the == operator.

avatar image Feref2 Hellium · Jun 12, 2019 at 09:01 PM 0
Share

Thanks now am using the ternary operator. Also, I found out that the 2nd condition in an if stament is not checked if the previous was true with OR: if (id == 0 || previousObject[id - 1].transform.localScale.x >= 1)

By the way, I was using the ?? solution, and I think it didn´t gave me any errors...?

avatar image SirPaddow · Jun 07, 2019 at 08:05 AM 1
Share

@Hellium is right, I didn't know that and didn't check it... $$anonymous$$y bad.

avatar image tormentoarmagedoom SirPaddow · Jun 07, 2019 at 08:36 AM 0
Share

Now is perfect, thanks @SirPaddow

Bye :D

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

110 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

Related Questions

Javascript GetComponent not work! 0 Answers

"NullReferenceException: Object reference not set to an instance of an object " only for one component 2 Answers

Disable function in another script 1 Answer

Tag control 0 Answers

How can i get component as an abstract base class? 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