- Home /
How to make a gameObject active triggered by a collision of another?
I'm trying to get Flower4 game object to become active after the player rolls over Flower3.
void OnCollisionEnter (Collision my_collision){
//print ("Collided with " + my_collision.collider.name);
if (my_collision.gameObject.name == "Bee") {
Debug.Log ("I'm A Bee!!");
}
if (my_collision.gameObject.name == "Flower1") {
UnityEngine.SceneManagement.SceneManager.LoadScene ("Level_Two");
}
if (my_collision.gameObject.name == "Flower2") {
UnityEngine.SceneManagement.SceneManager.LoadScene ("Level_Three");
if (my_collision.gameObject.name == "Flower3") {
(my_collision.collider.name == "Flower3"){
gameObject.SetActive(true)}
//if (my_collision.gameObject.name == "Flower4") {
//UnityEngine.SceneManagement.SceneManager.LoadScene ("Level_Four");}
}
}
}
$$anonymous$$ake unity Event and invoke event when player rolls over Flower3 and in his listener make gameObject active.
Answer by StrawberryJellyfish · Apr 25, 2017 at 10:25 AM
I'm supposing that you logic making Flower 4 active is Line 15-17? Because if it is, it doesn't make much sense to me. Typing gameObject.SetActive(true)
just enables the object that this script is attatched to.
I would suggest making a public variable flowerFour
for Flower 4 and using this code
if (my_collision.gameObject.name == "Flower3") {
flowerFour.SetActve(true);
}
I'm really new to Unity and I'm a bit confused on what to do. If you meant like this:
public class Collide : $$anonymous$$onoBehaviour {
public class flowerFour{
void OnCollisionEnter (Collision my_collision){
//print ("Collided with " + my_collision.collider.name);
if (my_collision.gameObject.name == "Bee") {
Debug.Log ("bee hit the orange flower");
}
if (my_collision.gameObject.name == "Flower1") {
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene ("Level_Two");
}
if (my_collision.gameObject.name == "Flower2") {
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene ("Level_Three");
if (my_collision.gameObject.name == "Flower3") {
flowerFour.SetActve(true);
}
if (my_collision.gameObject.name == "Flower4") {
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene ("Level_Four");}
}
}
}
}
Then it's not working.
No no, flowerFour
is meant to be a variable.
public class Collide : $$anonymous$$onoBehaviour {
public GameObject flowerFour;
void OnCollisionEnter (Collision my_collision){
//print ("Collided with " + my_collision.collider.name);
if (my_collision.gameObject.name == "Bee") {
Debug.Log ("bee hit the orange flower");
}
if (my_collision.gameObject.name == "Flower1") {
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene ("Level_Two");
}
if (my_collision.gameObject.name == "Flower2") {
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene ("Level_Three");
if (my_collision.gameObject.name == "Flower3") {
flowerFour.SetActve(true);
}
if (my_collision.gameObject.name == "Flower4") {
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene ("Level_Four");}
}
}
}
Then, In the Inspector, you drag and drop the Flower 4 gameObject into the slot that will be created in that script component.
Okay I tried that and it didn't work it seems.
public GameObject flowerFour;
void OnCollisionEnter (Collision my_collision){
//print ("Collided with " + my_collision.collider.name);
if (my_collision.gameObject.name == "Bee") {
Debug.Log ("bee hit the orange flower");
}
if (my_collision.gameObject.name == "Flower1") {
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene ("Level_Two");
}
if (my_collision.gameObject.name == "Flower2") {
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene ("Level_Three");
if (my_collision.gameObject.name == "Flower3") {
flowerFour.SetActive(true);
}
if (my_collision.gameObject.name == "Flower4") {
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene ("Level_Four");}
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter not working, tried everything! :( (C#) 3 Answers
Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers
How to stop Camera from going into colliders 3 Answers
Create A Counter And GameObjects Unique ID 1 Answer
[SCRIPTERS] Im making a building game with a system like Gmod 2 Answers