How can I activate part of the code in other script?
Attention: I don't need to activate all scripts. For example, a bunch of bullets flies around a player. And when one collide with a player, then need to activate part of the script of the collided object (that touched the player).
PlayerControl.cs
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Bullet1")
{
lasthit = 1f;
// I need to activate Destroyy() in Bullet1 script
}
}
Bullet1.cs
public void Destroyy()
{
Debug.Log("Destroyed!"); // I need to activate this part of the code
} // ONLY in Bullet1.cs
The code below shows how to do it:
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Bullet1")
{
lasthit = 1f;
// I need to activate Destroyy() in Bullet1 script
// HERE'S HOW:
if(col.gameObject.GetComponent<Bullet1>() != null)
col.gameObject.GetComponent<Bullet1>().Destroyy();
}
}
Just getComponent is be easier
void OnCollisionEnter2D(Collision2D col)
{
Bullet1 b = col.gameObject.GetComponent<Bullet1>();
if (b == null)
{
return;
}
lasthit = 1f;
b.Destroyy();
}
Actiovating bool in Bullet1 script
Player.cs
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Bullet1")
{
col.gameObject.GetComponent<Bullet1pt().booldestroy = true;
}
}
Bullet1.cs
public bool booldestroy;
private void Update()
{
if (booldestroy)
{
Destroyy();
}
}
Related
I am trying to get input when my player collides with another object. But the problem is i am not getting any input. I am getting inputs in update function but not in OnCollisonEnter2D,OnCollisionExit2D and OnCollisionStay2D.
Here is my script
public class PlayerInteractions : MonoBehaviour {
[SerializeField] private GameObject chestHelperMessage;
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
chestHelperMessage.SetActive(true);
if (Input.GetKeyDown(KeyCode.E)) {
Debug.Log("You got a weapon");
}
}
}
private void OnCollisionExit2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
chestHelperMessage.SetActive(false);
}
}
private void OnCollisionStay2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
if (Input.GetKeyDown(KeyCode.E)) {
Debug.Log("Chest opened");
}
}
}
}
none of the function is giving inputs.
Please help.
Thanks
for OnCollisonEnter2D and OnCollisionExit it is pretty unlikely that you press the key down exactly in the very same frame where the collision event happens.
Also for OnCollisionStay2D the call happens together with FixedUpdate, the physics routine. They are not happening every frame so it can happen that also here you miss the one or other input.
In general you should get user input in Update and do e.g.
// In case you need the chest reference itself
private GameObject currentChest;
IEnumerator CheckInputRoutine()
{
if (chestHelperMessage.activeSelf && Input.GetKeyDown(KeyCode.E))
// Or also
//if(currentChest && Input.GetKeyDown(KeyCode.E))
{
Debug.Log("You got a weapon");
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Chest"))
{
chestHelperMessage.SetActive(true);
currentChest = collision.gameObject;
StartCorouine (CheckInputRoutine());
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Chest"))
{
chestHelperMessage.SetActive(false);
currentChest = null;
StopAllCoroutines ();
}
}
This is a piece of code from my game that works for me:
void OnTriggerEnter(Collider other) // you can check if the collider is a player but I did not in this instance
{
HelperMessage.SetActive(true);
canOpenChest = true;
}
void Update()
{
if (canOpenChest && Input.GetKeyDown(KeyCode.E))
{
HelperMessage.SetActive(false); // set active to false because the chest is now open
Destroy(gameObject); // instead of this play an animation or whatever and, you can also remove the collider from the chest
}
}
void OnTriggerExit(Collider other)
{
Text.SetActive(false);
canPickup = false;
}`
The answer by derHugo is technically right but I don't like IEnumerators so I never use them
Thanks #RoberStan, the code given by you is not working for my game, but some changes made it work.
Here is the new code
// text on the screen to help the player which key to press to pick up the item
[SerializeField] private GameObject chestHelperMessage;
private GameObject currentChest;
private void CheckInput() {
// checking that if the helperMessage is active and getting key inputs
if (chestHelperMessage.activeSelf && Input.GetKeyDown(KeyCode.E)) {
Debug.Log("You got a weapon");
}
}
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
chestHelperMessage.SetActive(true);
currentChest = collision.gameObject;
}
}
private void OnCollisionExit2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
chestHelperMessage.SetActive(false);
currentChest = null;
}
}
private void Update() {
CheckKeyPress();
}
private void CheckKeyPress() {
if (chestHelperMessage.activeSelf) {
CheckInput();
}
}
I made this little script that should turn the light of a point light off and on ... unfortunately only the off button works, after which it doesn't turn on again .... maybe there is some error in the script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnLight : MonoBehaviour {
public GameObject light;
private bool on = true ;
void OnTriggerStay(Collider other) {
if (other.tag == "Player" && Input.GetKey(KeyCode.E) && !on)
{
light.SetActive(true);
on = true;
}
else if (other.tag == "Player" && Input.GetKey(KeyCode.E) && on)
{
light.SetActive(false);
on = false;
}
}
}
Please Check this link to understand OnTriggerStay Function
On Trigger Stay
Also Please check those to differentiate between
Get Key and
Get Key Down
the problem is calling of OnTriggerStay happens continuously and you are using GetKey functions which will return true all times if the player pressed.
So when you use GetKey this will cause Single press on E will be redden multiple times.
Here is my solution
public GameObject light;
bool isPlayerStandingNear = false;
private void Update()
{
if(isPlayerStandingNear)
{
//Debug.Log("isPlayerStandingNear");
if(Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Light State : "+ light.active);
light.SetActive(!light.active);
}
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
isPlayerStandingNear = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
isPlayerStandingNear = false;
}
}
I made a simple "radiation" field through a trigger but it works only when player in move. Function countes two decimal values.
What could be the problem?
decimal radIntensity = 0.001m;
decimal currentDose;
private void OnTriggerStay2D(Collider2D area)
{
if (area.gameObject.tag == "Player")
{
currentDose += radIntensity;
}
print($"Current dose: {currentDose} R");
}
In the RigidBody2D component there's an option to disable or enable the sleeping mode.
Try to disable it with the RigidbodySleepMode2D.NeverSleep option.
Also can use a boolean-flag and OnTriggerEnter2D and OnTriggerExit2D. But never sleeping rigidbody is much better
bool _flag;
private void Update()
{
if (_flag)
{
currentDose += radIntensity;
}
print($"Current dose {currentDose} R");
}
private void OnTriggerEnter2D(Collider2D area)
{
if (area.gameObject.tag == "Player")
{
_flag = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
_flag = !true;
}
I want to log in console when a cube passes through an another cube, the another cube has mesh collider with Convex and isTrigger is set to true.
using UnityEngine;
public class score_addations : MonoBehaviour
{
//[SerializeField]
//private int SCORE = 0;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "score")
{
Debug.Log("Pass");
}
else
{
Debug.Log("Fail");
}
}
private void Start()
{
//Cursor.lockState = CursorLockMode.Locked;
//Cursor.visible = false;
}
}
Here is an image of my game
for triggers you use OnTriggerEnter(Collider) Use this code
private void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "score")
{
Debug.Log("Pass");
}
else
{
Debug.Log("Fail");
}
}
I am trying to have my player walk onto a GameObject and if they are on that object and they press the space key show a debug log.
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Level_1" && Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Both Conditions Reached");
}
}
This would only trigger if they were holding down the space bar as they entered the object. You would do better to check if they were currently colliding with the object when the spacebar is pressed. (or do the check for both in Update)
Call this in your player object:
private void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Level_1") {
player.isInside = true;
}
}
private void OnTriggerExit2D(Collider2D other) {
if (other.gameObject.tag == "Level_1") {
player.isInside = false;
}
}
And use this to check for the spacebar:
public void Update() {
if (Input.GetKeyDown(KeyCode.Space) && player.isInside == true) {
Debug.Log("Both Conditions Reached");
}
}