• 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Get unity to recognize prefab in C# 2 Answers

Counter for sprite under script from GameObject prefab 0 Answers

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

Instantiate prefab with different values each time 4 Answers

How to run a function in a GO that have DontDestroyOnLoad 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges