Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by DoctorSauce · Jan 11, 2014 at 04:01 PM · instantiateoverlapping

Checking if object intersects?

Okay I thought I had this all figured out late last night, but coming back today I think I was dead wrong. And before you laugh at my script, I have only been doing my own scripting in C# for a little over a week. Anyway, I can't figure out how to check whether or not an object is overlapping another object, and based on that either delete the object or mark it as instantiated in another script. So here's the script:

 using UnityEngine;
 using System.Collections;
 
 public class AfterInstantiation : MonoBehaviour {
     private GameObject control;
     private GameManager manager;
     private bool overlapping = true;
 
     void Start(){
 
         if (!overlapping){
             Destroy (gameObject);
         }
 
         else {
             MarkAsInstantiated ();
         }
         
     }
 
     void MarkAsInstantiated(){
         control = GameObject.FindGameObjectWithTag ("Manager");
         manager = control.GetComponent <GameManager> ();
         GameManager.dungeonsExisting += 1;
     }
 
     bool Intersects(Bounds bounds){
         overlapping = bounds;
     }
 }

This script only gave me one error, so I decided to show this one. This is the error I get, if it helps:

Assets/Scripts/Level Generators/Corridors and Rooms/AfterInstantiation.cs(28,17): error CS0428: Cannot convert method group Intersects' to non-delegate type bool'. Consider using parentheses to invoke the method

I think the problem is I just don't know how to call variables from other functions or something simple, but could anybody help me figure out what's wrong with this script? Thank you in advance.

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 · Jan 11, 2014 at 04:15 PM

The problem is line 28. You are assigning a variable of type 'Bounds' to a variable of type 'bool'. Can't do that. I'm not sure what two bounds you are going to compare to get the intersection. The bounds of the collider maybe? Anyway the syntax to compare two bounds:

 overlapping = bounds1.Intersects(bounds2); 

It is strange to have a function both set an instance variable to a value and return the value, but maybe you want:

 bool Intersects(Bounds bounds){
    overlapping = collider.bounds.Intersects(bounds);
    return overlapping;
 } 
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 DoctorSauce · Jan 11, 2014 at 04:38 PM 0
Share

Okay I think this is getting me somewhere. So now there is this problem. I think I could use that first line of code you wrote as long as I could define the object for bounds2, but since the level is randomly generated, there will never be a set bounds2 object. Is there a way I could say something like:

 if (bounds1.Intersects(**any object**))

or maybe

 if (bounds1.Intersects(**any object with a certain tag**))
avatar image robertbu · Jan 11, 2014 at 05:27 PM 1
Share

You would have to first get a list of the objects (GameObject.FindGameObjectsWithTag()), and then test each one. As an alternate, take a look at Physics.OverlapSphere().

avatar image DoctorSauce · Jan 11, 2014 at 06:07 PM 0
Share

That Physics.OverlapSphere is awesome! I think I can get this to work, thank you very much for you help.

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

Object spawn issues 1 Answer

overlapping object doesn't disable 0 Answers

overlapping object doesn't disable 1 Answer

How to make objects spawn at the same time without colliding ? 1 Answer

Spawn GameObjects without overlap 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