- Home /
The question is answered, right answer was accepted
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!!
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;
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)
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.
Replace line #27 which is:
rigidbody2D.velocity = new Vector2(-maxSpeed, rigidbody2D.velocity.y);
with:
rb.velocity = new Vector2(-maxSpeed, rb.velocity.y);
Answer by Cherno · Jul 22, 2015 at 08:52 AM
Instead of
anim = GetComponentsInParent<Animator> ();
use
anim = GetComponentInParent<Animator> ();
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)
Well, if your parent object doesn't have a component you are trying to access, that is to be expectd, isn't it?
It does have a rigidbody2D
Here's what I'm trying to access, this is the inspector for the parent object
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
error CS0103 when I try to use Rigidbody2D 1 Answer