Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 question was closed Jul 22, 2015 at 10:48 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by animeman0 · Jul 22, 2015 at 08:42 AM · c#animatorrigidbody2dgetcomponent

error CS0029: Trying to access component in parent - c#

Hello there,

I'm trying to access the rigidbody2D and Animator in the parent of the gameobject this script is attached to.

However I get this: Assets/scripts/EnemyMoveLeft.cs(17,17): error CS0029: Cannot implicitly convert type UnityEngine.Animator[]' to UnityEngine.Animator'

Script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyMoveLeft : MonoBehaviour {
 
     public Rigidbody2D rb;
     public Animator anim;
     MovementController player;
 
     public Vector2 newPos;
     public Vector2 negativeNewPos;
     
     public float maxSpeed;
 
     void Start()
     {
         anim = GetComponentsInParent<Animator> ();
         rb = GetComponentInParent<Rigidbody2D> ();
         player = GameObject.FindGameObjectWithTag ("Player").GetComponent<MovementController> ();
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.isTrigger = !true && col.CompareTag ("Player"))
         {
             anim.SetFloat("Speed",Mathf.Abs(1));
             rigidbody2D.velocity = new Vector2(-maxSpeed, rigidbody2D.velocity.y);
         }
     }
 }

Is it something to do with the brackets around the animator?

Thanks in advance!!

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by HarshadK · Jul 22, 2015 at 08:54 AM

You are using GetComponentsInParent (notice 's' after component) to get Animator component which will return an array since it is used to get multiple components.

If you want to get just one component then use GetComponentInParent which you have used to get the Rigidbody2D component on parent. Or if you want to get multiple Animator components attached to parent then you should use GetComponentsInParent as you are doing currently but make your anim variable to be an array of Animator like:

 public Animator[] anim;
Comment
Add comment · Show 6 · 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 animeman0 · Jul 22, 2015 at 09:10 AM 0
Share

thank you, I should've noticed that.

avatar image animeman0 · Jul 22, 2015 at 09:12 AM 0
Share

however I get this now: $$anonymous$$issingComponentException: There is no 'Rigidbody2D' attached to the "DetectTriggerLeft" game object, but a script is trying to access it. You probably need to add a Rigidbody2D to the game object "DetectTriggerLeft". Or your script needs to check if the component is attached before using it. UnityEngine.Rigidbody2D.get_velocity () (at C:/buildslave/unity/build/artifacts/EditorGenerated/Physics2DBindings.cs:1194) Enemy$$anonymous$$oveLeft.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/scripts/Enemy$$anonymous$$oveLeft.cs:27)

avatar image HarshadK · Jul 22, 2015 at 10:30 AM 0
Share

On line 27 you are trying to access the Rigidbody component attached to the same gameobject to which this script is attached by using rigidbody2D. I guess you wanted to use rigidbody on parent using rb variable.

avatar image animeman0 · Jul 22, 2015 at 12:11 PM 0
Share

Yeah I do, so how would I Do that?

avatar image HarshadK · Jul 22, 2015 at 12:19 PM 0
Share

Replace line #27 which is:

 rigidbody2D.velocity = new Vector2(-maxSpeed, rigidbody2D.velocity.y);

with:

 rb.velocity = new Vector2(-maxSpeed, rb.velocity.y);
Show more comments
avatar image
1

Answer by Cherno · Jul 22, 2015 at 08:52 AM

Instead of

 anim = GetComponentsInParent<Animator> ();

use

 anim = GetComponentInParent<Animator> ();
Comment
Add comment · Show 4 · 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 animeman0 · Jul 22, 2015 at 09:10 AM 0
Share

thank you, I should've noticed that.

avatar image animeman0 · Jul 22, 2015 at 09:12 AM 0
Share

however I get this now: $$anonymous$$issingComponentException: There is no 'Rigidbody2D' attached to the "DetectTriggerLeft" game object, but a script is trying to access it. You probably need to add a Rigidbody2D to the game object "DetectTriggerLeft". Or your script needs to check if the component is attached before using it. UnityEngine.Rigidbody2D.get_velocity () (at C:/buildslave/unity/build/artifacts/EditorGenerated/Physics2DBindings.cs:1194) Enemy$$anonymous$$oveLeft.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/scripts/Enemy$$anonymous$$oveLeft.cs:27)

avatar image Cherno · Jul 22, 2015 at 10:23 AM 0
Share

Well, if your parent object doesn't have a component you are trying to access, that is to be expectd, isn't it?

avatar image animeman0 · Jul 22, 2015 at 10:26 AM 0
Share

It does have a rigidbody2D

Here's what I'm trying to access, this is the inspector for the parent object

alt text

rb-and-anim.png (49.5 kB)

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

error CS0103 when I try to use Rigidbody2D 1 Answer

Get Rigidbody2D component in script (c#) 1 Answer

Why isn't the animator getting my values? 0 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