Enemy instantiate agent not working properly - c#

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;
}
}
}

Related

Score Not Updating

I have been trying to make an OnTriggerEnter Score system and it has not been updating and is showing no errors so am I doing something wrong that I dont know about here is my Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ScoreManger : MonoBehaviour
{
public TextMeshProUGUI MyscoreText;
private int ScoreNum;
// Start is called before the first frame update
void Start()
{
ScoreNum = 0;
MyscoreText.text = ScoreNum.ToString();
}
void update() {
MyscoreText.text = ScoreNum.ToString();
}
public void OnTriggerEnte2D(Collider2D col){
if(col.tag == "Score"){
ScoreNum += 1;
Debug.Log("It Worked");
MyscoreText.text = ScoreNum.ToString();
Destory(col.gameObject);
}
}
}
You spelled OnTriggerEnter2D wrong.
Or you haven't marked at least one of the colliders you are interacting with as a trigger.
Or you haven't attached the script to a gameobject.

If statement works alone in void Start();

the if statement in PawnHasFallen() only works if its in the start funtion and he calls this logs Debug.Log($"{pawnName} has fallen", this); and Debug.Log("It Aint working mate"); for the pawn that hasnt fall yet. but i doesnt work when its called in the HitBowling script then i only get Debug.Log("It Aint working mate");. Why isnt it working when i call it in the other script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pawn : MonoBehaviour
{
private GameObject pawnName;
public Transform tf;
public Rigidbody rb;
[SerializeField]
float eulerAngX;
[SerializeField]
float eulerAngY;
[SerializeField]
float eulerAngZ;
public void Start()
{
tf = GetComponent<Transform>();
rb = GetComponent<Rigidbody>();
PawnHasFallen();
}
public void PawnHasFallen()
{
eulerAngX = transform.localEulerAngles.x;
eulerAngY = transform.localEulerAngles.y;
eulerAngZ = transform.localEulerAngles.z;
Debug.Log("Calling this script");
if (!Mathf.Approximately(Vector3.Angle(Vector3.up, transform.up), 0f))
{
Debug.Log($"{pawnName} has fallen", this);
}
else
{
Debug.Log("It Aint working mate");
}
}
}
The script where i call the other script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HitBowling : MonoBehaviour
{
public Pawn pawn;
public GameObject pawnPrefab;
public void Start()
{
GameObject obj = pawnPrefab;
pawn = obj.GetComponent<Pawn>();
}
public IEnumerator OnTriggerEnter(Collider collision)
{
Debug.Log("Big balls");
//if the ball hits the end of the bowling ally start this
if (collision.gameObject.name == "Ball")
{
Debug.Log("Start de second wait");
//wait 5 second for starting the fuction
yield return new WaitForSeconds(5);
Debug.Log("Ended the waiting");
//this function sees if the pawn has fallen and then gives you points
pawn.PawnHasFallen();
}
}
}

Why Update() doesnt set .text?

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.

Unity problem activating a function with parameters in camera from another script in GameObject Enemy

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

How do I make it so the player reactivates on replay?

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);
}

Categories

Resources