- Home /
Cannot implicitly convert type `UnityEngine.Transform' to `UnityEngine.Transform[]'
now what i am try to do is to create AI for enemy car in my racing game for that i have to create a path first so in order to do that i have written this script in which i am try to access child nodes of parent so for that i have created array and in which it is giving me error Transform[] pathTranform = GetComponentInChildren(); error is
Cannot implicitly convert type UnityEngine.Transform to UnityEngine.Transform[]
i am using this tutorial for path making https://www.youtube.com/watch?v=o1XOUkYUDZU please help me out
thanks in advance
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class path : MonoBehaviour {
public Color lineColor;
private List<Transform> nodes = new List<Transform>();
void OnDrawGizmos()
{
Gizmos.color = lineColor;
Transform[] pathTranform = GetComponentInChildren<Transform>(); // here is the error
nodes = new List<Transform>();
for(int i = 1; i < pathTranform.Length; i++)
{
if(pathTranform[1] != transform)
{
nodes.Add(pathTranform[i]);
}
}
for(int i = 0; i < nodes.Count; i++)
{
Vector3 CurrentNode = nodes[i].position;
Vector3 previousNode = Vector3.zero;
if(i > 0)
{
previousNode = nodes[i - 1].position;
}
else if(i == 0 && nodes.Count > 1)
{
previousNode = nodes[nodes.Count - 1].position;
}
Gizmos.DrawLine(previousNode, CurrentNode);
}
}
}
Answer by tanoshimi · Oct 09, 2016 at 08:39 AM
If you want to access components in children, plural, then you need to use GetComponentsInChildren.
Transform[] pathTranform = GetComponentsInChildren<Transform>();
thanks for replaying but i m using same in this script and still getting errors
Transform[] pathTranform = GetComponentInChildren<Transform>(); // here is the error
Look carefully. You want more than one component, so you need to use GetComponentsInChildren.
thanks tanoshimi :) it was a silly mistake GetComponentInChildren only s is missing GetComponentsInChildren
Your answer
![](https://koobas.hobune.stream/wayback/20220612094304im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to Create a list with initial capacity 5 Answers
Inventory System wont work 2 Answers
how to assign mesh.vertices ToList linq extension 1 Answer
How to properly create a 2 dimensional array of an object. [C#] 1 Answer