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
0
Question by Mightycam · Jan 17, 2015 at 02:56 PM · gameobjectcollidertriggeractivate

C# Make two objects collide to activate a third object

Basically I'm trying to test it out first. For the test I'm pushing a blue cube into a yellow cube to activate a green cube. I've created 2 scripts for each object to destroy once they collide. But I'm really stuck on how to make one object activate another on trigger. I can do players easily but I'm stuck on this now. I'm still learning all of this at the moment been about 2 months into it. So any good info on how to do this better will be great thanks.

 using UnityEngine;
 using System.Collections;
 
 public class ActivateGreen : MonoBehaviour 
 {
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "Blue")
         {
             other.gameObject.SetActive(false);
         }
     }
     
 }

and here's the second script

 using UnityEngine;
 using System.Collections;
 
 public class BlueDestroy : MonoBehaviour {
 
     // Use this for initialization
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "Yellow")
         {
             other.gameObject.SetActive(false);
         }
     }
 }
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 Natsu_k · Jan 17, 2015 at 03:06 PM 0
Share

You'll need to add more details about your scenario to get the correct answer. there is not enough information here to correctly deter$$anonymous$$e why your script isn't working. You need to include how they are being moved, what components they each have, are you using rigidbodies, are they set to kinematic.

These answers will help deter$$anonymous$$e the problem at hand.

avatar image Mightycam · Jan 17, 2015 at 03:24 PM 0
Share

objects are on rigidbody so I can push the objects into another. $$anonymous$$inematic isn't on either of them. And when I push both the yellow and blue cubes together, they both deactivate. I'm just having problems on how to get the green cube to activate when the blue and yellow collide. I've read tutorials and I really do struggle understanding scripting at the moment because 3 hour classes a week isn't much help so I try to do little exercises like this to help better my skills. Pretty much I want when blue and yellow are disabled the green cube gets activated.

3 Replies

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

Answer by Chris333 · Jan 17, 2015 at 07:08 PM

I guess that you want to create a game where u push two objects of different colors together to get a new one based on the 2 colors of the collided objects. A fusion look-a-like game with colors. Assuming that you want to create e.g. the green cube at the cords of the collions of the 2 other cubes, yellow and blue, you could use a Manager class(singleton-pattern) like its on the wiki http://wiki.unity3d.com/index.php/Singleton .

Than in the manager class you have a method for instantiating a cube of a different color based on the two collided cubes.

e.g.

 using UnityEngine;
 using System.Collections;
 
 public class Manager : Singleton<Manager>
 {
     protected Manager () {} // guarantee this will be always a singleton only - can't use the constructor!
     
     public GameObject greenCube;
     public GameObject blueCube;
     public GameObject yellowCube;
         
     public void InstantiateNewCube(MyCubeClass cubeOne, MyCubeClass cubetwo, Vector3 position)
     {
         if(cubeOne.color == "blue" && cubetwo.color == "yellow")
         {
             Instantiate (greenCube, position, Quaternion.identity);
         }
 
         //you need more methodes here for each fusion scenario of colors
     }
 }

Than you need a MyCubeClass with a variable which is storing the actual color. The MyCubeClass should be attached to the cubes. Additional you have to figure out the collision point or just use the position of one of the 2 cubes for the position of the new Cube. You can than call the method from everywhere using e.g

 void OnTriggerEnter(Collider other)
 {
      //get the components MyCubeClass of the two collided cubes(eg. use GetComponent<MyCubeClass>())
      //figuring out the position for the new cube
      Manager.Instance.InstantiateNewCube(cubeOne, cubetwo, newPosition);
 }

I didn't test it but thats the main idea. Using a gamePool would be even better to save performance instead of instantiating all the time a new cube.

Comment
Add comment · Show 2 · 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 Mightycam · Jan 18, 2015 at 12:27 AM 0
Share

Thanks a lot, I was using this as a test and practice little game ideas to better my skills in coding. It's hard when you only have one 3 hour class a week plus other subjects you must complete altogether.

avatar image Chris333 · Jan 18, 2015 at 12:38 AM 0
Share

Yeah i know that situation. In the universities you get mostly $$anonymous$$ched in the basics in a lots of fields and you have to work a lot in your free time to improve the skills in a specific field if you want to master it sometime.

avatar image
0

Answer by alok-kr-029 · Jan 17, 2015 at 03:16 PM

You code seems to be correct please provide more details about the scene and hope you have checked IsTrigger for the colliders in the inspector

Comment
Add comment · Show 1 · 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 Mightycam · Jan 17, 2015 at 03:28 PM 0
Share

triggers are working fine so that the blue and yellow get destroyed. But I need some kind of help or example just to help me understand how to make one object activate another. If I just do OnTriggerEnter, Is that for any object that enters the collider or just the player?

avatar image
0

Answer by einstienalbert787 · Apr 11, 2019 at 12:18 AM

Can you guys help, I basically want to collide 2 game objects when drag to initiate the third one, for example when I drag the pasta on plate these two object will destroy then pasta outcome will initiate..,Can you give a sample for this one ,like for example i have plate and pasta, of these 2 collided the pasta outcome will show.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Reaching a gameobject from its collider 3 Answers

Get All Renderers inside of GameObejct 0 Answers

GameObject going into terrain. 0 Answers

Problems with the triggers(not recognized) 1 Answer

Check if trigger collider is touching other trigger collider 2 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