I am making a really short C# Unity game for a college class I am in and I have created a script for a trap that deactivates my player on contact that also includes a replay button. It all works except when I replay, the player remains inactive.
How would I modify my script to make it so the player reactivates on replay?
Also, this class I am in is a beginner class, I'm not super good at this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Trap : MonoBehaviour
{
public GameObject playerExplosion;
public GameObject gameOverUI;
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
other.gameObject.SetActive(false);
gameObject.SetActive(false);
gameOverUI.SetActive(true);
PlayerController.gameOver = false;
}
}
Edit: Here is the replay script too. It works on a health bar system.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ReplayGame : MonoBehaviour
{
public Transform player;
public Image uiBar;
public GameObject GameOverUI;
public static Vector3 startPosition;
private float fillAmount;
void Start()
{
startPosition = player.position;
fillAmount = uiBar.fillAmount;
GameOverUI.SetActive(false);
}
public void Click ()
{
PlayerController.gameOver = false;
player.position = startPosition;
uiBar.fillAmount = fillAmount;
GameOverUI.SetActive(false);
}
}
you will have to re Activate your player, or you have to Instantiate a new player.
public void Replay ()
{
//ReActivate
myPlayer.gameObject.SetActive(true);
//or Instnatiate a new
Instantiate(myPlayer);
}
Related
I have got a problem when im trying to instantiate 2 kind of enemies. i want each of them follow each other and attack. the attack working good but the agent.SetDestination seem a bit buggy. not gaining any errors but when they do spawn they go to some position in the sence.
(if im not instantiate them and they on scene they work good, trying to make it work when spawn as well).
this is the basic spawn script
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class EnemySpawner: MonoBehaviour {
[SerializeField] private GameObject enemy1;
// [SerializeField] private GameObject enemy2;
private float spawnDelay=10f;
private float nextSpawnTime=10f;
void Update() {
if (ShouldSpawn()) {
SpawnEnemy();
}
}
private void SpawnEnemy() {
nextSpawnTime=Time.time+spawnDelay;
Instantiate(enemy1, transform.position, transform.rotation);
//Instantiate(enemy2, transform.position, transform.rotation);
}
private bool ShouldSpawn() {
return Time.time>=nextSpawnTime;
}
}
and this is the base move to the other Target aka other enemy.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyController: MonoBehaviour {
public Transform Target;
private float _speed=0.1f;
private NavMeshAgent _agent;
private void Awake() {
_agent=GetComponent<NavMeshAgent>();
}
void Start() {
StartCoroutine(FollowTarget());
}
private IEnumerator FollowTarget() {
WaitForSeconds wait=new WaitForSeconds(_speed);
while (enabled) {
_agent.SetDestination(Target.transform.position);
yield return wait;
}
}
}
hey i making my first game and I have a problem with coin piker script
can somebody tell mi why my script won't work on unity?
my script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinPicker : MonoBehaviour
{
public float coin = 0;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.transform.tag == "Coins")
{
Destroy(other.gameObject);
}
}
}
I have a Script Coin-Counter connected with a Text. Every time the Player and the Coin are colliding the coinScore decreases by 1. If I want to display the coinScore in Update() this doesn't work. Why?
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinScore : MonoBehaviour
{
[SerializeField] private Text coinScorer;
private int coinScore;
private int oldCoinScore;
void Update()
{
coinScorer.text = coinScore.ToString(); // This doesn't work.
oldCoinScore = coinScore;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
coinScore += 1;
Destroy(gameObject);
//coinScorer.text = coinScore.ToString(); //This works.
}
}
Your collision method is destroying this game object.
There is no update to run after that.
I have a script that makes the camera do a shake by putting a button because
It is a public access function, if I do it that way when placing a button it works well but what I cannot achieve is to call the function so that every time my player collides with an enemy he makes the shake. I hope you can help me.
The shake code in camera is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenShaker : MonoBehaviour {
private float shakeAmount = 0.5f;
private float shakeTime = 0.0f;
private Vector3 initialPosition;
private bool isScreenShaking = false;
void Update () {
if(shakeTime > 0)
{
this.transform.position = Random.insideUnitSphere * shakeAmount + initialPosition;
shakeTime -= Time.deltaTime;
}
else if(isScreenShaking)
{
isScreenShaking = false;
shakeTime = 0.0f;
this.transform.position = initialPosition;
}
}
public void ScreenShakeForTime(float time)
{
initialPosition = this.transform.position;
shakeTime = time;
isScreenShaking = true;
}
}
The enemy code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControladorEnemigoCirculoMediano : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Here I don't know what to call the public void function ScreenShakeForTime (float time);
I already tried many things online but when my character comes into contact with the character, I don't do the shake in the camera.
}
}
}
You can create Unity-singleton in your ScreenShaker class like:
class ScreenShaker
{
public static ScreenShaker Instance {private set; get;};
void Awake() {
Instance = this;
}
}
And than from any place to call like:
ScreenShaker.Instance.ScreenShakeForTime(2f);
This is the easiest way, but maybe it's better to create standard singeleton(it's up to you).
And also don;t forget to destroy it on OnDestroy()
can you tell me in enemy game object collider isTrigger is enable or not
if it is not enable then use OnColliderEnter2D(Collision2D other){} for collision detection
I've been trying to make a sliding door for my unity level and I've managed to set up the animations but the scripting that's supposed to link up the functions with the objects isn't working.
Here's the script for the key card:
using UnityEngine;
using System.Collections;
public class Key_Pickup_1 : MonoBehaviour {
public GameObject player;
private Player_inventory playerinventory;
void Awake ()
{
playerinventory = player.GetComponent<Player_inventory>();
}
// Update is called once per frame
void onTriggerEnter()
{
if (gameObject == player)
{
playerinventory.hasKey_1 = true;
Destroy(gameObject);
}
}
}
Here's the script for the Door animation:
using UnityEngine;
using System.Collections;
public class Door_Animation_1 : MonoBehaviour {
public string Open;
private Animator anim_1;
public GameObject player;
private Player_inventory playerInventory;
void Start()
{
anim_1 = GetComponent<Animator>();
player = GameObject.FindGameObjectWithTag("Player");
playerInventory = player.GetComponent<Player_inventory>();
}
void OntriggerEnter (Collider other)
{
if(other.gameObject == player)
{
if (playerInventory.hasKey_1)
{
anim_1.SetTrigger(Open);
}
}
}
Any Ideas?
You don't have the proper capitalization for the OnTriggerEnter methods in your code. You have two different spellings and they are both wrong. It must be exactly OnTriggerEnter (or OnTriggerEnter2D for objects with a Collider2D instead of a Collider).