• 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 Pauls · Apr 26, 2013 at 07:05 AM · gameobjectprefabgetcomponent

GetComponents error with GameObject?

Hi,

I don't understand this error :

BCE0149: The type 'UnityEngine.GameObject' must derive from 'UnityEngine.Component' in order to substitute the generic parameter 'T' in 'UnityEngine.GameObject.GetComponents.()'.

with this code in unityscript :

 just1Explode = Instantiate(Resources.Load("explode1"), vec, Quaternion.identity);
 
 var gos : GameObject[];
 //gos = just1Explode.GetComponents.();
     gos = just1Explode.GetComponents.<GameObject>();
 for (var jo : GameObject in gos) {
       Debug.Log("h");
 }


I have got a prefab with several gameobject inside, each with a rigidbody, and I would like to add a different impulse to each of them to create an explosion.

Thanks for your help

Comment
Add comment · Show 1
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 Pauls · Apr 26, 2013 at 07:30 AM 0
Share

@navadeep2011 thanks, sorry I made a mistake I edited my post, but I also tried with your code, and it says "Cannot infer generic arguments for method 'UnityEngine.GameObject.GetComponents.()'. Provide stronger type information through arguments, or explicitly state the generic arguments." When I provide the type "GameObject", the error is the one in my post... any idea?

3 Replies

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

Answer by whydoidoit · Apr 26, 2013 at 08:03 AM

GameObject isn't a component and cannot be got that way - in any case, all of the components share the game object you are calling it on!

What you actually want to do to get all of the rigidbodies so you can add the force is:

   var rigidbodies = just1Explote.GetComponentsInChildren.<Rigidbody>();
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 Pauls · Apr 26, 2013 at 05:12 PM 0
Share

@whydoitdoit thanks a lot

avatar image
1

Answer by KiraSensei · Apr 26, 2013 at 07:50 AM

GetComponents returns a list of Components.

 just1Explode = Instantiate(Resources.Load("explode1"), vec, Quaternion.identity);
  
 var gos : Component[];
 gos = just1Explode.GetComponents();
 
 for (var jo : Component in gos) {
     Debug.Log("h");
 }

EDIT : I just saw that you said that just1Explode has children, so you have to loop on its children and then search the right component in them. So the right method to use should be GetComponentsInChildren()

 just1Explode = Instantiate(Resources.Load("explode1"), vec, Quaternion.identity);
      
     var gos : Component[];
     gos = just1Explode.GetComponentsInChildren<YourComponent>();
     
     for (var jo : Component in gos) {
         Debug.Log("h");
     }
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 Pauls · Apr 26, 2013 at 05:12 PM 0
Share

@$$anonymous$$iraSensei thanks a lot $$anonymous$$iraSensei

avatar image
1

Answer by Ailaxgt · Apr 26, 2013 at 07:50 AM

The problem is with the line.

 gos = just1Explode.GetComponents.<GameObject>();

You are trying to get Components of type GameOject but GameObject is not a Component. I hope the following code helps. :) My JS is close to non-existant.

 var gos : MyExplosingScript[];
     gos = just1Explode.GetComponents.<MyExplosingScript>();
     for (var jo : MyExplosingScript in gos) {
         Debug.Log("h");
         jo.StartMyAwesomeExplosion();
         }
     }
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 Pauls · Apr 26, 2013 at 05:13 PM 0
Share

@Ailaxgt thanks a lot Ailaxgt

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

15 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

Related Questions

Counter for sprite under script from GameObject prefab 0 Answers

Get unity to recognize prefab in C# 2 Answers

How do I assign a script to an instantiated prefab? 2 Answers

How to Access Components in Children of GameObject? 1 Answer

Accessing prefab components 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