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 alemimi · Dec 21, 2013 at 05:47 PM · instantiatecollidernotworking

Check if an Instantiated object is colliding with another object right after being instantiated

I'm trying to code a procedural corridor generator, there are 3 types of corridor, 1 is the straight one, 2 is the right turn one and 3 is the left turn one.

I placed 2 Empty GameObjects as Parent and Child respectively of the corridor, the parent one is the fixed axis, and the child one is the spawnpoint of the new corridor.

I've run into a problem, when it generates the corridors, somethimes they intersect, so to solve this I tried writing a code that checks if the GameObject is colliding with another GameObject, if the corridor is colliding destroy it and retry creating another one, until it finds a solution; I tried both OnCollisionEnter and OnTriggerEnter and nothing, it doesn't check, I tried making a Debug.Log but it's not writing anything in the Console.

Here is the code to generate the corridors:

 using UnityEngine;
 using System.Collections;
 
 public class ProceduralObjects : MonoBehaviour {
 
     private GameObject[] corridorTypes = new GameObject[3];
     private int randomSpawn;
     public static bool generate = false;
     public GameObject spawnPoint;
     private bool instantiate1Time = true;
 
     // Use this for initialization
     void Start () {
         GameObject corridorType1 = (GameObject)Resources.Load("Corridoio1Spawner", typeof(GameObject));
         GameObject corridorType2 = (GameObject)Resources.Load("Corridoio2Spawner", typeof(GameObject));
         GameObject corridorType3 = (GameObject)Resources.Load("Corridoio3Spawner", typeof(GameObject));
         corridorTypes[0] = corridorType1;
         corridorTypes[1] = corridorType2;
         corridorTypes[2] = corridorType3;
 
     }
     
     // Update is called once per frame
     void Update () {
         randomSpawn = Random.Range(0, corridorTypes.Length);
         if(generate && instantiate1Time){
             Instantiate(corridorTypes[(randomSpawn)], spawnPoint.transform.position, spawnPoint.transform.rotation);
             StartGame.temporaryCorridorGenerated += 1;
             instantiate1Time = false;
         }
     }
 
     void CollisionWithOtherWall(){
         StartGame.temporaryCorridorGenerated -= 1;
         Destroy(gameObject);
     }
 
 }

And here is the code to send the message to destroy the GameObject (This script is attached to the child of the Empty GameObject):

 using UnityEngine;
 using System.Collections;
 
 public class ProceduralObjectsChild : MonoBehaviour {
 
     void OnTriggerEnter(Collider other){
         if(other.gameObject.tag == "Wall"){
             SendMessageUpwards("CollisionWithOtherWall", SendMessageOptions.DontRequireReceiver);
             Debug.Log("Collision With Other Wall");
         }
     }
 }

The problem, I think, is that Unity is not checking if the object, right after being instantiated, is colliding or not.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Dec 21, 2013 at 05:54 PM

I think, is that Unity is not checking if the object, right after being instantiated, is colliding or not.

Correct. You have to wait a frame to get your collision. Not a problem I personally have had to solve, but the typical solutions posted on this list include keeping the object invisible and waiting a frame to see if there is a collision. Or you can use Physics.CheckSphere() or Physics.OverlapSphere() (which both will return some false positives). Or you can use some sort of casting such as Physics.Raycast(). Or knowing the physical properties of your objects, calculate the collision yourself.

Comment
Add comment · Show 3 · 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 alemimi · Dec 21, 2013 at 06:46 PM 0
Share

Okay I got it, but it's the same thing, it's not doing anything;

I looked up in Google how to wait a frame, and ended up with this code:

 public class ProceduralObjectsChild : $$anonymous$$onoBehaviour {

 GameObject other2;

 void OnTriggerEnter(Collider other){
     other2 = other.gameObject;
     StartCoroutine("Wait1Frame");
 }

 IEnumerator Wait1Frame(){
     yield return 0;

     if(other2.gameObject.tag == "Wall"){
         Send$$anonymous$$essageUpwards("CollisionWithOtherWall", Send$$anonymous$$essageOptions.DontRequireReceiver);
         Debug.Log("Collision With Other Wall");
     }
 }

}

avatar image robertbu · Dec 21, 2013 at 07:14 PM 0
Share

As mentioned, it's not something I've done before. So I played for a few $$anonymous$$utes. The solution I came up with is have the collider on the object you are placing have 'IsTrigger' set to true (you can change it later). Then this code worked:

 using UnityEngine;
 using System.Collections;
 
 public class Bug25c : $$anonymous$$onoBehaviour {
     public bool collided = false;
 
     void Start() {
         StartCoroutine(CheckForCollision());
     }
 
     void OnTriggerEnter() {
         collided = true;
     }
 
     IEnumerator CheckForCollision() {
         yield return null;
         Debug.Log (collided);
 
     }
 }

Note OnCollisionEnter() worked, but then you get the physics interaction between the two objects.

avatar image alemimi · Dec 21, 2013 at 07:50 PM 0
Share

Okay, that pretty much worked, now I got to find out why the objects are deleting themselves at each frame, just creating and deleting, creating and deleting, weird, maybe they are colliding with each other, thanks for the Code!

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

18 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 avatar image avatar image avatar image

Related Questions

OnTriggerEnter not working 0 Answers

WTF is wrong! OnTrigger, OnCollision, nothing working! 2 Answers

splatPrototypes not changing in exported game 0 Answers

Lights only work when I look at the ground. 0 Answers

Set bounds of collider added at runtime 1 Answer


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