Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 MegaChuck64 · Jul 29, 2015 at 12:50 AM · collisiongameobjectarraydefine

trying to define the last object I collided with and store it as a variable?

I want to take whatever object i just collided with and save it as "obj1". and if "obj1" is taken, i want to save it as "obj2". I tried this but it doesn't do anything. any suggestions?

 using System.Collections;
 
 public class DuplicationScript : MonoBehaviour {
 
     public GameObject obj1;
     public GameObject obj2;
     GameObject newParentObj;
     Rigidbody rB1;
     Rigidbody rB2;
     
     public bool hasFixedJoint;
     public bool noHit;
     public bool hasHit;

     void Start () {
         rB1 = obj1.GetComponent<Rigidbody>();
         rB2 = obj2.GetComponent<Rigidbody>();
         noHit = true;
     }
 
     void Update () {
         if (Input.GetKeyDown(KeyCode.F) && hasFixedJoint == false)
         {
             obj1.AddComponent<FixedJoint>();
             hasFixedJoint = true;
             rB1.mass = .5f;
             rB2.mass = .5f;
         }
     
         if (hasFixedJoint)
         {
             obj1.GetComponent<FixedJoint>().connectedBody   = obj2.GetComponent<Rigidbody>();
         }
     }
     
     void OnCollisionEnter(Collision hit){
         if (noHit)
         {
             obj1 = hit.gameObject;
             hasHit = true;
         }
         if (hasHit)
         {
             obj2 = hit.gameObject ;
         }
     }
 }
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 TonanBora · Jul 29, 2015 at 01:06 AM 0
Share

Try adding a Debug.Log statement that prints the name of the object you collided with before storing it in either of the obj variables.

Also, make sure that the GameObject with this script has a collider (not any of its children), otherwise it will not detect a collision.

avatar image MegaChuck64 · Jul 29, 2015 at 01:25 AM 0
Share

yeah i tried messing around with that, i couldn't really find a useful way to use debug.log in this situation.

2 Replies

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

Answer by meat5000 · Jul 29, 2015 at 02:18 AM

 void OnCollisionEnter(Collision hit)
 { 
     if (!obj1)
     {
         obj1 = hit.gameObject;
     }
     else if(!obj2)
     {
         obj2 = hit.gameObject;
     }
 }

This needs no boolean flags.

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 TonanBora · Jul 29, 2015 at 02:04 AM

I am not sure this has anything to do with your problem, but a better way of structuring your Collision method is like this:

 if(noHit){
   obj1 = hit.gameObject;
   hasHit = true;
   notHit = false; // You forgot this line
 }else{ // if noHit is false
  obj2 =- hit.gameObject;
 }

In this code, if noHit is true, it sets the colliding gameObject to obj1, otherwise it sets it to obj2. The problem with your original code was that since noHit was not getting set to false, the second Game Object that caused a collision would get set to both obj1 and obj2 because both If statements would be checked and both would be true.

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Detecting if object in array collides with another object in same array 1 Answer

Collecting Array items in order 1 Answer

Collision with my first object of array or second... 1 Answer

Sphere detection system 2 Answers

Use an objects (from array) position to focus a camera on 3 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