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);
}
}
}
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;
}
}
}
I am making a game where, if the catcher gets destroyed by the object, the game over screen is triggered. All that seems to occur is that there is a giant game over the screen at the beginning of when I play, while the game is running in the background. For some reason, the game does not seem to call in the game over screen only on collision.
This is the script I am using for my catcher, where it collides, disappears, and then the game over screen is set up to be triggered.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen;
}
}
}
and this is the code for my GameOverScreen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public void Setup(int score)
{
gameObject.SetActive(true);
pointsText.text = "Score:" + score.ToString();
}
}
You are not calling anything when an object collides, you're just listing the reference of the object. You'll need to call the function that you have exposed. Without calling a method from the script reference, no code will be run. Edit your first snippet as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.Setup();
}
}
}
The other option would be to move the code in Setup to Awake or Start or OnEnable, then instead of calling the function in the collision, you just need to set it as active.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.score = theScore;
GameOverScreen.gameObject.SetActive(true);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public int score;
private void OnEnable()
{
pointsText.text = "Score:" + score.ToString();
}
}
The one issue is you'll need to pass in the score parameter which I do not see in your script CatcherDestroy.
We are trying to add the ability to lose three child slimes at the same time when attacked by a specific weapon.
Children's slime is managed by List.
List script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChildSlimeList : MonoBehaviour
{
[SerializeField]
private List<GameObject> SlimeChild=new List<GameObject>();
public void ChildSlimeRandomOff()
{
for (int i = 0; i < 2; i++)
{
var SlimeNum = Random.Range(0, SlimeChild.Count);
GameObject SlimeChildList = SlimeChild[SlimeNum];
SlimeChildList.SetActive(false);
SlimeChild.RemoveAt(SlimeNum);
}
}
}
Child slime script
using System;
using System.Collections.Generic;
using _SlimeCatch.Stage;
using UnityEngine;
using Random = System.Random;
public class ChildrenSlimeWeaponCollider : MonoBehaviour
{
[SerializeField] private GameObject MolotovCocktail;
public GameObject GameManager;
ChildSlimeList _childSlimeList;
void Start()
{
//_childSlimeList = GetComponent<ChildSlimeList>().SlimeChild();
}
public void OnCollisionEnter2D(Collision2D other)
{
//if (!other.gameObject.CompareTag("Weapon")) return;
//_childSlimeList.SlimeChild.RemoveAt(this.gameObject);
if (other.gameObject.CompareTag("MolotovCocktail"))
{
GameManager.GetComponent<ChildSlimeList>().ChildSlimeRandomOff();
}
Destroy(gameObject);//or SetActive(false)
Destroy(other.gameObject);//or SetActive(false)
}
}
At this rate, the elements of the slime list of the child attacked by a specific weapon will not be deleted directly. Help me.
State of the gameenter image description here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChildSlimeList : MonoBehaviour
{
[SerializeField]
private List<GameObject> SlimeChild=new List<GameObject>();
public void ChildSlimeRandomOff()
{
for (int i = 0; i < 2; i++)
{
var SlimeNum = Random.Range(0, SlimeChild.Count);
GameObject SlimeChildList = SlimeChild[SlimeNum];
SlimeChildList.SetActive(false);
SlimeChild.RemoveAt(SlimeNum);
}
}
public void SlimeColliderDecision(GameObject gameObject)
{
SlimeChild.Remove(gameObject);
}
}
using System;
using System.Collections.Generic;
using _SlimeCatch.Stage;
using UnityEngine;
using Random = System.Random;
public class ChildrenSlimeWeaponCollider : MonoBehaviour
{
[SerializeField] private GameObject MolotovCocktail;
public GameObject GameManager;
ChildSlimeList _childSlimeList;
void Start()
{
//_childSlimeList = GetComponent<ChildSlimeList>().SlimeChild();
}
public void OnCollisionEnter2D(Collision2D other)
{
//if (!other.gameObject.CompareTag("Weapon")) return;
if (other.gameObject.CompareTag("MolotovCocktail"))
{
GameManager.GetComponent<ChildSlimeList>().SlimeColliderDecision(this.gameObject);
GameManager.GetComponent<ChildSlimeList>().ChildSlimeRandomOff();
}
gameObject.SetActive(false);
other.gameObject.SetActive(false);
}
}
By making this change, we were able to hide three objects and at the same time reduce the number of elements in the list that contain them.
Many thanks to those who gave me tips.
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 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);
}