- Home /
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
{
myList.Add(child);
}
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
}
}
Up voted so you get the ability to post comment, and have to use answers. If anyone else is reading, consider giving question asker an up vote. Think they need two votes to get the ability to comment
Comments can be used at any karma level. $$anonymous$$any people just choose to lurk insufficiently before using the answer field.
Answer by DaveA · Apr 16, 2013 at 04:59 AM
Several problems here.
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.
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.
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).
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.
Answer by Zels · Apr 16, 2013 at 06:32 AM
I would have to do some research into character controller never used it before.
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? while(iwhile(i<4) { Transform child = transform.GetChild(i);
} I tried using the collider but it didnt detect any walls
Answer by powerproc_chris · Apr 16, 2013 at 02:18 PM
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:
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 offoreach
usage (used as a scripting example in Unity):public class ForeachExample : MonoBehaviour { List<string> manyStrings = new List<string>(); void Start() { manyStrings.Add("Apple"); manyStrings.Add("Green"); manyStrings.Add("Monkey"); } void Update() { foreach(string str in manyStrings) { Debug.Log("Hello! My string is " + str); } } }
You are trying to convert an object with type
Transform
into aGameObject
foreach(Transform child in walls)
(assuming of course that walls is now aList
orGameObject[]
, an array ofGameObject
). This is not possible. Notice in my example above that the type of theList
isstring
and I'm also using that same type in theforeach
loop:foreach(string str in manyStrings)
.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 ofGameObject
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! =)
Yeah that doesn't help I have already learned in previous labs. $$anonymous$$y major problem is with collision and foreach loops on how to get the children to be added to the list but yet still to be able to check if my character gameobject has collided with something in the list.
Okay well there are still those issues with your code that I explained. Those will need to be fixed first. Other than that, I wouldn't recommend doing collision checks yourself. Unity has built-in things like that such as CharacterController. Using that Component on your game objects (player, enemies, projectiles, etc.), including a RigidBody on those game objects, and having a collider on your walls will solve your problem.
I figured out the conversion error I have tried using colliders before but it didn't work but, I think I figured out why I have some different errors from a prefab and a gameobject that I need to figure out.
If you have more errors that need to be solved, I suggest either asking a new question or update this one. Whichever is appropriate given the situation.
Your answer
Follow this Question
Related Questions
How to adjust two colliders really close to each other w/o them colliding? 1 Answer
Unity Height Glitch (explained) 1 Answer
My object is going through boundaries Problem! 1 Answer
Set bounds of collider added at runtime 1 Answer
OnCollision events are not called between two rigidbody colliders 0 Answers