- Home /
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);
}
}
}
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.
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.
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.
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.
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.
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
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?
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.
Your answer
Follow this Question
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