- Home /
Question by
wolga2 · Sep 29, 2013 at 02:54 PM ·
charactercontrollerlistforeachindexgetcomponentinchildren
Access Components forech GameObject in List
I have this code and it seems not to work. Is this really the right way to access and affect every CharacterController of the its gameobject in the List? And also will I really get the different index for each gameObject in the list this way? Thanks a lot!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class FollowOther : MonoBehaviour
{
privat int aNumber = 10;
private Vector3 moveVector = Vector3.zero;
private float gravity = 20f;
private List <GameObject> gameObjects;
void Start ()
{
gameObject = GameObject.FindGameObjectWithTag("Tag").ToList();
}
void Update ()
{
forech (gameObject in gameObjects)
{
CharacterController controller = gameObject.GetComponent<CharacterController>();
if (!controller.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime); //that should in add the gravity for each gameObject if it's not grounded.
}
int index = gameObjects.IndexOf(gameObject);
int newNumber = aNumber - index; //that should in case of every gameObject be another value
}
}
}
Comment
it surely has to be: (foreach GameObject gameObject in gameObjects) sorry, wrote that wrongly.