Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Zels · Apr 16, 2013 at 04:49 AM · collisioncolliderboundsforeachmaze

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
         }
     }
     
     
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Linus · Apr 16, 2013 at 02:14 PM 0
Share

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

avatar image Loius · Apr 16, 2013 at 03:37 PM 0
Share

Comments can be used at any karma level. $$anonymous$$any people just choose to lurk insufficiently before using the answer field.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Apr 16, 2013 at 04:59 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Zels · Apr 16, 2013 at 06:32 AM

  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? while(iwhile(i<4) { Transform child = transform.GetChild(i);

} I tried using the collider but it didnt detect any walls

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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:

  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() {
                 manyStrings.Add("Apple");
                 manyStrings.Add("Green");
                 manyStrings.Add("Monkey");
             }
     
             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! =)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zels · Apr 16, 2013 at 03:02 PM 0
Share

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.

avatar image powerproc_chris · Apr 16, 2013 at 03:08 PM 0
Share

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.

avatar image Zels · Apr 16, 2013 at 03:51 PM 0
Share

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.

avatar image powerproc_chris · Apr 16, 2013 at 03:56 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges