- Home /
Question by
aniket_vs · Apr 28, 2020 at 01:44 PM ·
gameobjecttransformnullreferenceexceptiongetcomponenttransform.position
NULL REFERENCE EXCEPTION,NULL REFERENCE EXCEPTION while accessing child objects unity2D
I have a set of bones which follow the IK CCD solver and I am trying to move the position of the first bone with respect to the last bone using transform. For this I am trying to access the Child Objects of the the main body, but I keep getting NULL REFERENCE EXCEPTION
public class LevelSnake : MonoBehaviour
{
private GameObject outputonlinepngtools;
private Transform lastbone;
private Transform boneone;
// Start is called before the first frame update
void Start()
{
outputonlinepngtools = gameObject ;
boneone = GameObject.Find("bone_1").GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
Transform[] alltransforms = outputonlinepngtools.GetComponentsInChildren<Transform>();
foreach (Transform t in alltransforms){
if(t.gameObject.name == "bone_23"){
lastbone.transform.position = t.gameObject.transform.position ;
}
if(t.gameObject.name == "bone_1"){
//boneone.transform.position = lastbone.transform.position;
t.gameObject.transform.position = lastbone.transform.position;
}
}
}
}
Comment