Unity中名为conversation的UI GameObject无法正常激活
问题:UI对象
conversation无法在碰撞触发时激活 在Unity项目的Player脚本中,OnCollisionEnter2D函数内存在异常:当碰撞体标签为PredaconSpawn且cheetorr.dead为true时,terrosaur与waspinator可成功通过SetActive(true)激活,但名为conversation的UI GameObject无法被激活。替换为其他UI GameObject后问题仍未解决。
相关代码片段
private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = true; } if (collision.gameObject.CompareTag("Stratosphere")) { SceneManager.LoadScene("Peru"); inPeru = true; } if (collision.gameObject.CompareTag("Talk")) { scourgeTalk.SetActive(true); } if (collision.gameObject.CompareTag("PredaconSpawn") && cheetorr.dead) { terrosaur.SetActive(true); waspinator.SetActive(true); conversation.SetActive(true); } }
完整Player脚本代码
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Player : MonoBehaviour { public bool hasTransformed = false; [HideInInspector] public float horizontal; public int speed = 5; public int jumpVel = 10; public bool isGrounded = true; public Rigidbody2D rb; public GameObject bodyParts; public GameObject bodyParts1; public GameObject bodyParts2; public Animator anim; public int speed1 = 10; public BoxCollider2D robotCollider; public float timerToChange = 2f; public bool decrease = false; public bool decrease1 = false; public GameObject car; public BoxCollider2D carCollider; public int ammo = 20; public float timerToReload = 3f; public GameObject shootParticle; public bool decrease2 = false; public float timerToParticle = 1.5f; public bool particle = false; public bool inPeru = false; public GameObject scourgeTalk; public Animator cheetor; public GameManager gm; public Cheetor cheetorr; public GameObject terrosaur; public GameObject waspinator; public bool cheetorWalk = true; public bool right; public GameObject conversation; public bool left; void Start() { } void Update() { horizontal = Input.GetAxis("Horizontal"); if (!hasTransformed) { transform.position += new Vector3(horizontal, 0f, 0f) * speed * Time.deltaTime; }else if (hasTransformed) { transform.position += new Vector3(horizontal, 0f, 0f) * speed1 * Time.deltaTime; } if (!hasTransformed) { Jump(); } Transform(); Animate(); if (cheetorWalk) { CheetorWalkAnim(); } } void CheetorWalkAnim() { if(horizontal == 0 && gm.done) { cheetor.SetBool("run", false); } else { cheetor.SetBool("run", true); } } void Animate() { if(horizontal == 0) { anim.SetBool("run", false); } else { anim.SetBool("run", true); } if (horizontal < 0) { transform.eulerAngles = new Vector3(0, 180, 0); right = true; left = false; } if (horizontal > 0) { transform.eulerAngles = new Vector3(0, 0, 0); left = true; right = false; } } void Transform() { if(Input.GetKey(KeyCode.Space) && hasTransformed) { anim.SetTrigger("retransform"); decrease1 = true; timerToChange = 1.7f; hasTransformed = false; } if (Input.GetKey(KeyCode.T) && !hasTransformed) { anim.SetTrigger("transform"); decrease = true; timerToChange = 1.7f; hasTransformed = true; } if (decrease) { timerToChange -= 1 * Time.deltaTime; } if (decrease1) { timerToChange -= 1 * Time.deltaTime; } if(timerToChange <= 0 && hasTransformed) { bodyParts.SetActive(false); bodyParts1.SetActive(false); bodyParts2.SetActive(false); robotCollider.isTrigger = true; hasTransformed = true; car.SetActive(true); carCollider.isTrigger = false; } if (timerToChange <= 0 && !hasTransformed) { bodyParts.SetActive(true); bodyParts1.SetActive(true); bodyParts2.SetActive(true); robotCollider.isTrigger = false; hasTransformed = false; car.SetActive(false); carCollider.isTrigger = true; } } void Jump() { if(Input.GetButtonDown("Jump") && isGrounded) { isGrounded = false; rb.AddForce(Vector2.up * jumpVel, ForceMode2D.Impulse); } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = true; } if (collision.gameObject.CompareTag("Stratosphere")) { SceneManager.LoadScene("Peru"); inPeru = true; } if (collision.gameObject.CompareTag("Talk")) { scourgeTalk.SetActive(true); } if (collision.gameObject.CompareTag("PredaconSpawn") && cheetorr.dead) { terrosaur.SetActive(true); waspinator.SetActive(true); conversation.SetActive(true); } } }
排查与解决建议
- 确认对象引用:检查Inspector面板中Player组件的
conversation变量是否已正确赋值,无缺失引用。 - 检查UI层级与遮挡:将
conversation的Sorting Order设为临时最大值,排查是否被其他UI遮挡;同时确认其所在Canvas的渲染层级设置正常。 - 验证父物体状态:检查
conversation的所有父GameObject是否处于激活状态,父物体未激活时子物体无法显示。 - 排查逻辑冲突:搜索项目中所有修改
conversation激活状态的代码,确认没有其他脚本在同一时刻将其设为false。 - 添加调试日志:在碰撞触发代码中加入日志,验证代码分支是否执行、对象是否为空:
if (collision.gameObject.CompareTag("PredaconSpawn") && cheetorr.dead) { Debug.Log("触发PredaconSpawn碰撞,cheetorr.dead状态:" + cheetorr.dead); Debug.Log("conversation对象是否为空:" + (conversation == null)); terrosaur.SetActive(true); waspinator.SetActive(true); conversation.SetActive(true); Debug.Log("conversation当前激活状态:" + conversation.activeSelf); } - 适配Canvas渲染模式:若Canvas为World Space模式,确认
conversation的位置在相机可视范围内,避免因位置错误导致不可见。
内容的提问来源于stack exchange,提问作者AmoghUrs
相关产品推荐
相关产品推荐

