OnCollisionEnter() not working when two GameObjects collide - c#

This code here doesn't work, I don't get any errors, but the scripts doesn't print "End" in the console when my player hits something, here's the code:
using UnityEngine;
public class GameManager : MonoBehaviour
{
public void End(){
Debug.Log("End");
}
}
and on another script:
using UnityEngine;
public class PlayerCollision3 : MonoBehaviour
{
public Move movement;
void OnCollisionEnter (Collision collisionInfo) {
if (collisionInfo.collider.tag == "obsik"){
movement.enabled = false;
FindObjectOfType<GameManager>().End();
}
}
}

Related

How can I redirectveriable veriable from object to my haracter

I have 2 scripts one on player :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPositionCorrection : MonoBehaviour
{
Transform _playerTransform;
public float _xAxys;
public float _newXAxys;
public GameObject _changerPlayerPosition;
private void Start()
{
_playerTransform = GetComponent<Transform>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "ChangePlayerPosition")
{
float _newXAxys = this.GetComponent<ChangePositionOn>()._newPostion;
}
}
private void LateUpdate()
{
if (transform.position.z != 0)
{
transform.position = new Vector3(_xAxys, _playerTransform.position.y, _playerTransform.position.z);
}
}
and second on object :
public class ChangePositionOn : MonoBehaviour
{
public float _newPostion = 5;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
I using Unity 2022.1.19f1 and C#.
Thank you for your healp,
Michal
I would like to have several object in my game and when player will collide with them change location on x axis.
Unfortunately every time i have this error message:
NullReferenceException: Object reference not set to an instance of an object
PlayerPositionCorrection.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerPositionCorrection.cs:23)
Check if the ChangePositionOn script is attached to the player
gameObject.
remove the float decalaration in the function because it makes it a
local variable
replace your function with this instead
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("ChangePlayerPosition"))
{
Debug.Log("Check if Collided With : "+ other.gameObject.name);
_newXAxys = this.GetComponent<ChangePositionOn>()._newPostion;
Debug.Log("New XAxys value : "+ _newXAxys );
}
}
Make a new script PositionCorrector and Attach it to the Collider GameObject :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PositionCorrector : MonoBehaviour
{
//this should be attached to Collider GameObject
//this is the X value you want to change to, each collider can have a different number.
public float _newXAxys=4;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("PlayerTag"))
{
//changing the x axys of the player position
other.transform.position = new Vector3(_newXAxys, other.transform.position.y, other.transform.position.z);
}
}
}

Assets\EndTriger.cs(6,1): error CS8803: Top-level statements must precede namespace and type declarations

So i made a starter game from brackeys and i get 3 errors please help me
Script:GameManager
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public bool gameHasEnded = false;
public float restartDelay = 1f;
public void CompleteLevel ()
{
Debug.Log("LEVEL COMPLETE");
}
public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("Restart", restartDelay);
}
}
void Restart ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
script:EndTriger
using UnityEngine;
public class EndTriger : MonoBehaviour
public GameManager gameManager;
{
void OnTriggerEnter()
{
gameManager.CompleteLevel();
}
}
EndTriger:
using UnityEngine;
public class EndTriger : MonoBehaviour
{
public GameManager gameManager;
void OnTriggerEnter()
{
gameManager.CompleteLevel();
}
}

Animation keeps getting triggered when I step on the tiles

I'm developing a TopDown 2D game on Unity with some RPG elements and I did as so, when the player steps on a set of tiles placed on the map, it triggers an animation with a UI showing some text. However, the animation gets called multiple times while the player keeps stepping on the tiles and even when he exits the trigger area.
What I need help with is how to make the animation only trigger once and, while the UI is in on the screen, the player is not allowed to move until the player presses the button in the UI (already programmed and working).
Here is the code I used to make the trigger function:
public class TriggerScript : MonoBehaviour
{
public string popUp;
public Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
pop.PopUp(popUp);
Debug.Log("trigger");
animator.SetTrigger("pop");
}
}
And here is the PopUpSystem that sets the text:
public class PopUpSystem : MonoBehaviour
{
public GameObject popUpBox;
public Animator popupanimation;
public TMP_Text popUpText;
public void PopUp(string text)
{
popUpBox.SetActive(true);
popUpText.text = text;
popupanimation.SetTrigger("pop");
}
}
If, in order to help me, you need more information and details, please ask me in the comment section.
Note that I am new to Unity and have zero experience with it so, if you can be patient and explain things in a simple way, I would enjoy that!
Thank you for reading.
Edit: this is the animator window:
Edit 2: The code that I use for the movement of the player:
public class PLayerController : MonoBehaviour
{
private Rigidbody2D MyRB;
private Animator myAnim;
public string popUp;
[SerializeField]
private float speed;
// Start is called before the first frame update
void Start()
{
MyRB = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
}
private void Move()
{
myAnim.SetTrigger("popUp");
}
// Update is called once per frame
void Update()
{
MyRB.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * speed * Time.deltaTime;
myAnim.SetFloat("moveX", MyRB.velocity.x);
myAnim.SetFloat("moveY", MyRB.velocity.y);
if ((Input.GetAxisRaw("Horizontal")==1) ||(Input.GetAxisRaw("Horizontal") == -1)||(Input.GetAxisRaw("Vertical") == 1)||(Input.GetAxisRaw("Vertical") == -1))
{
myAnim.SetFloat("LastMoveX", Input.GetAxisRaw("Horizontal"));
myAnim.SetFloat("LastMoveY", Input.GetAxisRaw("Vertical"));
}
}
}
Edit 3: Code with boolean:
public class TriggerScript : MonoBehaviour
{
public string popUp;
public Animator animator;
bool condition = false;
void Start()
{
animator = GetComponent<Animator>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (condition == false)
{
PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
pop.PopUp(popUp);
Debug.Log("trigger");
animator.SetTrigger("pop");
condition = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
animator.SetTrigger("close");
}
All you have to do is to set your TriggerScript to something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TriggerScript : MonoBehaviour
{
public string popUp;
private void OnTriggerEnter2D(Collider2D collision)
{
PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
if (collision.gameObject.tag == "Player")
{
pop.PopUp(popUp);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
PopUpSystem pop = GameObject.FindGameObjectWithTag("GameManager").GetComponent<PopUpSystem>();
pop.closeBox();
}
}
and then set your PopUpSystemScript to something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PopUpSystem : MonoBehaviour
{
public GameObject popUpBox;
public Animator popupanimation;
public TMP_Text popUpText;
public void PopUp(string text)
{
popUpBox.SetActive(true);
popUpText.text = text;
popupanimation.SetTrigger("pop");
}
public void closeBox()
{
popupanimation.SetTrigger("close");
}
}
Now click on the popUp and close animation clips (in Animations folder) and remove the Loop Time check mark. and you should be all set to see the animation appears and disappears.
To stop the player, you can either use Time.timeScale. or set the rigidbody to isKenimatic.

Is there a way to check if a GameObject has been destroyed?

I'm creating a game and i want to show a panel when the player is dead
I've tried different approaches but none seems to do what I want
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DeadOrAlive : MonoBehaviour
{
public GameObject Player;
public GameObject deadPanel;
void Update()
{
if (!GameObject.FindWithTag("Player"))
{
deadPanel.SetActive(true);
}
}
}
To check if a object has been destroyed, you should use MonoBehavior's OnDestroy like so:
// Attach this script to the player object
public class DeadOrAlive : MonoBehaviour
{
public GameObject deadPanel;
void OnDestroy()
{
deadPanel.SetActive(true);
}
}
You can also instead of destroying the player object, set it to active/inactive, but to check if the player is dead or alive this way, you will need a separate object which checks the active state:
//Attach this to a object which isn't a child of the player, maybe a dummy object called "PlayerMonitor" which is always active
public class DeadOrAlive : MonoBehaviour
{
public GameObject deadPanel;
void Update()
{
if (!GameObject.FindWithTag("Player"))
{
deadPanel.SetActive(true);
}
}
}
Haven't used unity in a while and forgot how weird it could get.
Thanks to #VincentBree this is how I did it
void Update()
{
if (!Player.activeSelf)
{
deadPanel.SetActive(true);
}
}

GameObject not Spawining

I am making a tetris game with unity but this code is not spawning my tetrimino. I have assigned my gameobjects to the group[] array.
This is my code:
using UnityEngine;
using System.Collections;
public class Spawner1 : MonoBehaviour {
public GameObject[] group;
void start(){
SpawnNext ();
}
void SpawnNext(){
Instantiate(group[Random.Range(0,group.Length)],new Vector2(5.0f,10.0f),Quaternion.identity);
}
}
It's not spawning because you used lower-case s in start. This should be Start not start. Please fix that and your object should now start spawning.
public class Spawner1 : MonoBehaviour {
public GameObject[] group;
void Start(){
SpawnNext ();
}
void SpawnNext(){
Instantiate(group[Random.Range(0,group.Length)],new Vector2(5.0f,10.0f),Quaternion.identity);
}
}

Categories