Having Converision error with foreach loops

I basically making a maze which I manually created the walls I am trying to access all of its children then put it into a list in order to check for collision with my character gameobject

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Collision_1 : MonoBehaviour {

Vector3 currentPosition;
GameObject walls = GameObject.Find("Wall1");
public List myList = new List();

void Update()
	foreach(Transform child in walls) <-does not like this b/c of some conversion error
	foreach (GameObject walls in myList) here it say if I use it this way walls will have a diffrent meaning
		if (walls.collider.bounds.Contains(transform.position))
			//move character out of wall

Several problems here.

  1. What are you trying to do? Are you trying to not go into a wall? In which case make sure there’s a collider on each wall and you’re using a character controller.
  2. You are adding all those children on every Update call, which happens many times per second. And for no good reason. And not deleting them.
  3. You can iterate through the children of walls.transform instead of putting them in a list (they are already in a list, the list of children of walls).
  4. You could use OnCollisionEnter to detect when the transform enters a wall bounds. The system checks this for you in a much more efficient way.
  1. I would have to do some research into character controller never used it before.

  2. What would you suggest just take the for eachloop where I add it to the list out of update?

So you mean something like this?
Transform child = transform.GetChild(i);

I tried using the collider but it didnt detect any walls

Keep in mind that I’m not trying to bash on you or “bring you down” (so to speak) with this answer. It seems like it would probably be a good idea to learn a bit more about programming first. I’m going to explain why:

  1. You are trying to use a foreach loop on a single GameObject. foreach “repeats a group of embedded statements for each element in an array or an object collection.” GameObject is only a single object, not a list of objects. Example of foreach usage (used as a scripting example in Unity):

    public class ForeachExample : MonoBehaviour {
        List<string> manyStrings = new List<string>();
        void Start() {
        void Update() {
            foreach(string str in manyStrings) {
                Debug.Log("Hello! My string is " + str);
  2. You are trying to convert an object with type Transform into a GameObject foreach(Transform child in walls) (assuming of course that walls is now a List or GameObject[], an array of GameObject). This is not possible. Notice in my example above that the type of the List is string and I’m also using that same type in the foreach loop: foreach(string str in manyStrings).

  3. There is no type specified for your List object. Your type of object is the same type of object that you are trying to add to the list. Therefore, if you want to have a List of GameObject then your List will look like this:

    List<GameObject> gameObjects = new List<GameObject>();

I highly recommend going through some basic tutorials on programming and building your way up. Keep in mind that this is not meant to bash on you but rather to give some good advice (hopefully). A good place to start would be here: Scripting Tutorials for C# and Unity. I haven’t viewed them myself, as I went to college for a major in Computer Science, but I would think that they would be able to help you quite well. Unity’s support and tutorials has only been ever increasing as the product itself grows. I hope this helps! =)