Unity 5 OnCollisionEnter IF statement not working.
Help.. what's wrong with my script!!! im just trying to make a script that when the player collides with a cube or trigger it will start a function of if statement but never happens here's my script.. I will appreciate who answer this thank you LOVE UNITY!!!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class scriptB : MonoBehaviour {
public GameObject cube1;
public GameObject parent;
public Button pickUpAndPlace;
public Button place;
void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "trigger2") {
test();
}
if(Input.GetKeyDown("space")) {
print("Hello World");
}
}
void test () {
cube1.transform.parent = parent.transform;
}
}
is "trigger2" your object's name or your object's tag ? if its a tag then you should change this line col.gameObject.name == "trigger2" as col.gameObject.tag == "trigger2"
furthermore
If you dont have rigidbody on objects that contains your script add a rigidbody.
If you checked your cube's collider component as trigger you should rewrite OnCollisionEnter as OnTriggerEnter
Answer by Cuttlas-U · Apr 07, 2017 at 09:21 AM
hi if u want to use a "Input.GetKeyDown() " function u should use it in TriggerStay like this :
void OnCollisionStay(Collision col)
{
if (Input.GetKeyDown(KeyCode.Space))
{
print("Hello World");
}
}
and for this u better use trigger and not Collision:
void OnTriggerStay(Collider col)
{
if (Input.GetKeyDown(KeyCode.Space))
{
print("Hello World");
}
}