- Home /
Find Child of GameObject from Array in Different Script
Ok so I dont know if this is possible but what I need to do is to find the Child of a GameObject that is referenced in one script from the array of another script. So to clarify, I fill an array in Script 1 with GameObjects.(all these gameobjects have same children of each other but are not instances).Script 2 gets Script 1 and then finds the GameObject in the array that it collided with. Now I need the child's position of the collided with GameObject. Any thoughts...
Script 1
public class Chain : MonoBehaviour { public GameObject[] chainLinks = new GameObject[6]; public GameObject linkBegin; public int pos = 0; // Use this for initialization void Start () { // First Link that all chains will follow chainLinks[pos] = linkBegin; pos++; } // Update is called once per frame void Update () { } void OnTriggerExit(Collider collisionInfo) { if(collisionInfo.gameObject.tag == "Link"){ chainLinks[pos] = collisionInfo.gameObject; pos++; } } }
Script 2
// Getting Chain.cs arrayList
Chain chainScript = GameObject.FindWithTag("Player").GetComponent<Chain>();
for(int i =0;i<chainScript.chainLinks.Length;i++){
print(chainScript.chainLinks.Length);
print(chainScript.chainLinks[i]);
if(chainScript.chainLinks[i] == this.gameObject){
//targ = chainScript.chainLinks[i-1].GetComponentInChildren<GameObject>();
foreach(GameObject x in targ){
if(x.name == "NextLinkFollower"){
//target = x.transform;
}
}
//target = targ.Find("/NextLinkFollower").gameObject.transform;
//target = targo.transform;
break;
}
}
Where /NextLinkFollower is the child I am trying to get of the "Chain" scripts gameobject
Thanks
Your answer
Follow this Question
Related Questions
Iterate through the list of child objects within a game objects 2 Answers
Creating an array of immediate children of a game object -1 Answers
Use an objects (from array) position to focus a camera on 3 Answers
Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 Answers
Detection if a GameObject is below you or next to you? 1 Answer