// Update is called once per frame void Update () { if (!isAlive) {
RestartButton.SetActive (true); isAlive = true; }
scoreText.text = "Score:" + score;
} public static void Score (string animal) {
switch (animal) { case "Animal1(Clone)": score += 5; break; case "Animal2(Clone)": score += 100; break; case "Animal3(Clone)": score += 25; break; }
} /// <summary> /// OnCollisionEnter is called when this collider/rigidbody has begun /// touching another rigidbody/collider. /// </summary> /// <param name="other">The Collision data associated with this collision.</param> void OnCollisionEnter (Collision other) { if (other.gameObject.name == "Animal1(Clone)") score += 5; } public static void Die () { isAlive = false; } public void Restart () { RestartButton.SetActive (false); score = 0; SceneManager.LoadScene (0); }
Sorun şurada galiba, "public static void Score (string animal)" fonksiyonu tanımlamışsınız ama bu fonksiyonu hiçbir yerde çağırmamışsınız. Yani skoru değiştireceğiniz yerlerde daha doğrusu bu fonksiyonun içindeki switch-case'in çalışması gereken yerlerde "Score(isim)" şeklinde fonksiyonu çağırmanız gerek. "isim" yazdığım yere işte ne yazılacaksa mesela siz Animal1(Clone) falan yazmışsınız, o şekilde dışarıdan parametreyi verip çağırmalısınız.
Ya da komple o işlemleri fonksiyon olarak kullanmayacaksanız fonksiyon içinde tanımlamadan direk istediğiniz yerde switch-case'i yapın.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SpawnManager : MonoBehaviour {
public GameObject[] animalPrefabs = new GameObject[3];
private float spawnRangeX = 17f;
private float spawnZ = 20f;
private float startDelay = 2f;
private float spawnInterval = 1.2f;
public static int score = 0;
public Text scoreText;
public static bool isAlive = true;
public GameObject RestartButton;
void Start () {
InvokeRepeating ("SpawnRandomAnimals", startDelay, spawnInterval);
}
// Update is called once per frame
void Update () {
if (!isAlive) {
RestartButton.SetActive (true);
isAlive = true;
}
scoreText.text = "Score:" + score;
}
public static void Score (string animal) {
switch (animal) {
case "Animal1(Clone)":
score += 5;
break;
case "Animal2(Clone)":
score += 100;
break;
case "Animal3(Clone)":
score += 25;
break;
}
}
/// <summary>
/// OnCollisionEnter is called when this collider/rigidbody has begun
/// touching another rigidbody/collider.
/// </summary>
/// <param name="other">The Collision data associated with this collision.</param>
void OnCollisionEnter (Collision other) {
if (other.gameObject.name == "Animal1(Clone)")
score += 5;
}
public static void Die () {
isAlive = false;
}
public void Restart () {
RestartButton.SetActive (false);
score = 0;
SceneManager.LoadScene (0);
}
void SpawnRandomAnimals () {
int animalIndex = Random.Range (0, animalPrefabs.Length);
Vector3 spawnPos = new Vector3 (Random.Range (-spawnRangeX, spawnRangeX), 0, spawnZ);
Instantiate (animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation);
}
}
< Bu mesaj bu kişi tarafından değiştirildi El-HusseinBinHattab -- 21 Haziran 2020; 19:18:48 >