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 darkmwar · Jul 06, 2014 at 02:28 AM · gameobjectinstantiatec++

Unable to call another gameObject's script

I have some objects instantiated, I call on one of these object and try to access it's script but unity can't seems to find it.

The object has the script A. and one of the function is

 class A(){
 
 public void b(){
 ......
 }
 }

so I do,

 class{
 
 GameObject temp;
 
 temp = ........, successfully retrieved the gameObject I want to work on,
 
 temp.gameObject.GetComponents<A>().b(); //this doesnt work. 
 }

gives me the following error:

Assets/Scripts/colliders/LeftCollider.cs(42,88): error CS1061: Type A[]' does not contain a definition for b' and no extension method hit' of type A[]' could be found (are you missing a using directive or an assembly reference?)

I stripped down the code for simplicity. I've tried temp.GetComponents().b();//still no go, the logic behind this is that temp is already a gameObject.

I've also tried temp.gameObject.GetComponents;//monodevelope doesn't show red, but still get a compile error

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

4 Replies

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

Answer by MKayJay · Jul 06, 2014 at 03:32 PM

I think the problem is that you are using GetComponents (with an s) and not GetComponent.

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 darkmwar · Jul 06, 2014 at 05:59 PM 0
Share

That did it! Thanks

avatar image
1
Best Answer

Answer by Nick4 · Jul 06, 2014 at 03:35 PM

To access a specific method of a class you should use GetComponent() method not GetComponents(). Because GetComponents() returns an array of components of Type.

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 darkmwar · Jul 06, 2014 at 05:59 PM 0
Share

That did it! Thanks

avatar image
1

Answer by Kolodej · Jul 06, 2014 at 03:16 PM

Hi, class A must inherit from MonoBehaviour

 class A : MonoBehaviour {
    public B() { Debug.Log("Im doing B"); }
 }

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 Nick4 · Jul 06, 2014 at 03:32 PM 0
Share

Nice catch :) but he probably forgot to write here

avatar image
0

Answer by Kiwasi · Jul 06, 2014 at 02:31 AM

Those two little [] in the error message give the game away. Whatever code you are using to set temp is returning an array of GameObjects, not a single GameObject. In order to solve you can change the temp function to provide a single GameObject, or you can refer to individual GameObject using an index.

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 darkmwar · Jul 06, 2014 at 04:18 AM 0
Share

but game objects were stored in a queue, not a list. And when I destroy temp, it only destroy that objects, not all the objects.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Combine objects instantiated at runtime 2 Answers

Instantiate as child 3 Answers

Split a cube into several pieces? 1 Answer

Get transform of an instantiated GameObject(clone) 1 Answer

AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 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