Unity -动作被调用两次- OnTriggerEnter, OnClick,一切?



所以我正在创建一个绵羊计数游戏,昨天,当我上床睡觉的时候,一切都很好。今天,当我打开Unity做一些最后的润色时,我所做的一切都被调用了两次……

所以当我点击开始按钮时,它会调用开始游戏两次,然后当我的羊碰到障碍物时,它会调用OnTriggerEnter两次。我不知道我做错了什么,或者发生了什么,我宁愿不重新启动整个项目…

控制台日志<<p>计数器脚本/strong>

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Counter : MonoBehaviour
{
public TMP_Text savedText;
public TMP_Text deadText;
private int savedCount = 0;
private int deadCount = 0;
private void Start()
{
savedCount = 0;
deadCount = 0;
}
public void AddSavedSheep()
{
savedCount++;
savedText.text = "Saved: " + savedCount;
}
public void AddDeadSheep()
{
deadCount++;
deadText.text = "Dead: " + deadCount;
}
}
<<p>羊脚本/strong>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SheepControls : MonoBehaviour
{
private Rigidbody rb;
[Header("Movement")]
[Tooltip("How fast to move GameObject Forward.")]
public float forwardSpeed = 4.0f;
[Tooltip("Apply this much force to Rigidbody.")]
public float jumpForce = 5.0f;
private float groundY;
[Space]
[SerializeField] private bool jumping;
[SerializeField] private bool isDestroyed;
[SerializeField] private bool isSaved;
public ParticleSystem explosionParticles;
private Counter counter;
void Start()
{
rb = GetComponent<Rigidbody>();
groundY = (transform.position.y)+0.02f;
counter = FindObjectOfType<Counter>();

}
void Update()
{
transform.Translate(forwardSpeed * Time.deltaTime * Vector3.forward);

if (Input.GetKeyDown(KeyCode.Space) && !jumping)
{
if(transform.position.y < groundY)
{
Jump();
}
}
jumping = false;
}
private void Jump()
{
jumping = true;
rb.AddForce(Vector3.up * jumpForce);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("ExplosiveWire"))
{
if (isDestroyed) return;
else
{
Debug.Log("Hit Wire");
isDestroyed = true;
Destroy(gameObject);             
Instantiate(explosionParticles, transform.position,
explosionParticles.transform.rotation);    
}
counter.AddDeadSheep();
}
if (other.CompareTag("Goal"))
{
if (isSaved) return;
else
{
Debug.Log("Reached Goal");
isSaved = true;
Destroy(gameObject);
}
counter.AddSavedSheep();
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Sheep"))
{
Physics.IgnoreCollision(this.GetComponent<Collider>(),
collision.gameObject.GetComponent<Collider>());
}
}
}
<<p>GameManager脚本/strong>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public bool isGameActive;
public GameObject titleScreen;
public GameObject sheepControllerPrefab;
public void StartGame(int difficulty)
{
titleScreen.SetActive(false);
isGameActive = true;
InvokeRepeating(nameof(SpawnSheepWave), 2.0f, 2.0f);
}
public void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void SpawnSheepWave()
{
Instantiate(sheepControllerPrefab, transform.position, 
transform.rotation);
}
}

Start Button Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DifficultyButton : MonoBehaviour
{
private Button button;
private GameManager gameManager;
[Header("Difficulty Level")]
[Tooltip("The spawn rate will be divided by this number.")]
public int difficulty;
void Start()
{
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
button = GetComponent<Button>();
button.onClick.AddListener(SetDifficulty);
}
public void SetDifficulty()
{
Debug.Log(gameObject.name + " was clicked.");
gameManager.StartGame(difficulty);
}
}

所以,它实际上添加了两次分数,所以最初我想也许我需要做一个isDestroyed或isSaved检查,并将其添加到脚本中,但似乎没有什么能阻止它添加两次分数,或者认为我点击了两次开始按钮。

分数的双倍增量

我觉得我的代码很好,我只是不知道它发生了什么,为什么它调用了两次。你认为是否存在解决方法,或者我可能需要在新项目中重新创造这款游戏?

尝试禁用盒子碰撞器时,它进入触发器可能解决问题?

绵羊上只有一个BoxCollider,栅栏上有一个BoxCollider,球门上有一个BoxCollider。它们都有刚体,它们的叫声都是正确的,它们只是一次发生两次。

感谢你们提供的任何帮助!

我还尝试在if语句之前或之后移动增量步骤,看看它是否会做任何不同的事情,但我得到相同的结果。

private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("ExplosiveWire"))
{
if (isDestroyed) return;
else
{
Debug.Log("Hit Wire");
isDestroyed = true;
Destroy(gameObject);             
Instantiate(explosionParticles, transform.position,
explosionParticles.transform.rotation);
counter.AddDeadSheep();
} 
}
if (other.CompareTag("Goal"))
{
if (isSaved) return;
else
{
Debug.Log("Reached Goal");
isSaved = true;
Destroy(gameObject);
counter.AddSavedSheep();
}
}
}

[编辑]增加了附加到游戏对象的脚本图像。附加到游戏对象的脚本

每次点击你的难度按钮。它已经运行了。得到2。查看开始按钮检查器中的选项

最新更新