• 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 /
  • Help Room /
avatar image
0
Question by DougAllen · Sep 04, 2018 at 01:23 PM · collisioncollidersboolaccessing from any script

How to check if an object has collided from another script?

 public bool touchCheck;
 private bool self;
 private bool notSelf;


 void OnTriggerEnter(Collider other) {

     if (other.gameObject.tag == "Piece") {

         if (other.transform == gameObject.transform.parent) {
             self = true;
         } else if (other.transform != gameObject.transform.parent) {
             notSelf = true;
         }

         if (self && notSelf) {
             touchCheck = true;
             Debug.Log ("Pieces touch!");
         }
     }
 }

The above is the "TouchCheck" script attached to a child object, checking if it collides with it's parent and another object at the same time. This script works. My problem is this: I want a separate script "RoomGen" to tell me what "TouchChecks" value is. When I get to the point in my "RoomGen" script that asks whether there is a collision before it spawns another piece, it's value will NEVER return true despite the "TouchCheck" collision script saying there has been a collision.

I would appreciate any help as I haven't been able to figure this out for two days.

 public GameObject Piece01, Piece02, Piece03, Piece04, Piece05, Piece06, Piece07, Piece08, Piece09; 
 private GameObject nextSpawnObj;
 private GameObject spawnObject;
 GameObject touchCheckObj;

 public int maxPieces;
 int spawnCount;
 int rndPieceSpawn;
 Vector3 nextSpawnPos;
 Quaternion nextSpawnRotation;


 void Start () {
     
     nextSpawnPos = Vector3.zero;
     nextSpawnRotation = new Quaternion();
 }

 void Update () {

     if (spawnCount != maxPieces) {
         
         rndPieceSpawn = Random.Range (1, 10);

         switch (rndPieceSpawn) {
         case 1:
             spawnObject = Piece01;
             break;
         case 2:
             spawnObject = Piece02;
             break;
         case 3:
             spawnObject = Piece03;
             break;
         case 4:
             spawnObject = Piece04;
             break;
         case 5:
             spawnObject = Piece05;
             break;
         case 6:
             spawnObject = Piece06;
             break;
         case 7:
             spawnObject = Piece07;
             break;
         case 8:
             spawnObject = Piece08;
             break;
         case 9:
             spawnObject = Piece09;
             break;
         }

         SpawnObj (spawnObject);

         touchCheckObj = GameObject.Find("touchCheck");
         TouchCheck touchCheckScript = touchCheckObj.GetComponent<TouchCheck>();
         bool check = touchCheckScript.touchCheck;
             
         if (check == true) {
             Debug.Log ("Pieces touch! - RoomGen");
         }

         if (check == false) { 
         FindNewSpawnPos ("nextObjSpawnPoint");
         FindNewSpawnRota ("nextObjSpawnPoint");
         Destroy (touchCheckObj.gameObject);
         spawnCount += 1;
     }
             
 }
     

}

 private void SpawnObj(GameObject Obj) {

     GameObject.Instantiate (Obj,nextSpawnPos, nextSpawnRotation);
 }

 public void FindNewSpawnPos (string nextObjSpawnPoint) {
     
     nextSpawnObj = GameObject.Find (nextObjSpawnPoint);
     nextSpawnPos = nextSpawnObj.transform.position;
 }

 public void FindNewSpawnRota (string nextObjSpawnPoint) {

     nextSpawnObj = GameObject.Find (nextObjSpawnPoint);
     nextSpawnRotation = nextSpawnObj.transform.rotation;
     Destroy (nextSpawnObj);
 }
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
0

Answer by Rynbernsz · Sep 04, 2018 at 07:03 PM

In your RoomGen, create a TouchCheck object, something like this.

 TouchCheck check;
 
 void Start() {
 
      // Sets check object to touchCheck boolean 
      check = GameObject<TouchCheck>().touchCheck;
 }
 
 void Update() {
      // Use touchCheck boolean
      if (touchCheck) {
           
           // Do this if touchCheck is TRUE
      } else {
 
           // Do this instead if touchCheck is FALSE
      }
 }

I don't know if this is most optimal, but this is what I do usually. Let me know if that works! :)

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

How can I detect the collision of a gameObject which is at a high speed? 1 Answer

Making my walls disappear by touching the floor 0 Answers

hi guys am having a problem with my player 0 Answers

RigidBody Collisions Detect 0 Answers

Destroy GameObject inconsistent with Instantiated children 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