我与Unity合作,我开始了一个小项目,我使用了一个框架来发明。
我试图在游戏中建立一个安全区域。我的安全区有一个碰撞器“触发”。
如果我的玩家进入安全区,我只想发送消息。 我的问题是,当玩家进入时,我无法得到玩家的名字,它会给我“_TriggerHandler”作为值而不是我的玩家。
代码永远不会进入“if”,我在控制台中获得第一个调试日志,但没有其他内容。 (p为null)(other = _TriggerHandler)
无法理解为什么其他= _TriggerHandler。
using Devdog.General;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SafeZone : MonoBehaviour {
protected vp_FPPlayerEventHandler m_Player = null;
protected bool safe;
// Use this for initialization
void Start()
{
m_Player = GameObject.Find("Gringan").GetComponent<vp_FPPlayerEventHandler>();
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// registers this component with the event handler (if any)
/// </summary>
protected virtual void OnEnable()
{
if (m_Player != null)
m_Player.Register(this);
}
/// <summary>
/// unregisters this component from the event handler (if any)
/// </summary>
protected virtual void OnDisable()
{
if (m_Player != null)
m_Player.Unregister(this);
}
void OnTriggerEnter(Collider other)
{
var p = other.GetComponent<Player>(); // Player is in Devdog.General;
print("Collision detected with trigger object " + other.name + " p= " + p);
if (p != null)
{
print("Collision detected with trigger object PLAYER " + other.name);
safe = true;
m_Player.PlayerIsSafe.Send(safe);
}
}
void OnTriggerStay(Collider other)
{
var p = other.GetComponent<Player>(); // Player is in Devdog.General;
if (p != null)
{
print("Still colliding with trigger object PLAYER " + other.name);
safe = true;
m_Player.PlayerIsSafe.Send(safe);
}
}
void OnTriggerExit(Collider other)
{
var p = other.GetComponent<Player>(); // Player is in Devdog.General;
if (p != null)
{
print(gameObject.name + " and trigger object PLAYER " + other.name + " are no longer colliding");
safe = false;
m_Player.PlayerIsSafe.Send(safe);
}
}
}
控制台消息:
- &GT;
使用触发对象检测到碰撞_TriggerHandler p = UnityEngine.MonoBehaviour:打印(对象) SafeZone:OnTriggerEnter(Collider)(在Assets / My Assets / Scripts / SafeZone.cs:44)
我想知道是否有人知道会发生什么事情?
感谢您的帮助。
答案 0 :(得分:0)
好的我做到了:),不得不使用other.transform.parent.GetComponent();就像说Nika Kasradze一样,然后我必须在做其他任何事情之前测试玩家是否为null:)
感谢您的帮助!
using Devdog.General;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SafeZone : MonoBehaviour {
protected vp_FPPlayerEventHandler m_Player = null;
protected bool safe;
protected Player player;
// Use this for initialization
void Start()
{
if (m_Player == null)
{
m_Player = GameObject.Find("Gringan").GetComponent<vp_FPPlayerEventHandler>();
}
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// registers this component with the event handler (if any)
/// </summary>
protected virtual void OnEnable()
{
if (m_Player != null)
m_Player.Register(this);
}
/// <summary>
/// unregisters this component from the event handler (if any)
/// </summary>
protected virtual void OnDisable()
{
if (m_Player != null)
m_Player.Unregister(this);
}
void OnTriggerEnter(Collider other)
{
if (player == null)
{
player = other.transform.parent.GetComponent<Player>(); // Player is in Devdog.General;
}
if (player != null)
{
print("Collision detected with trigger object " + player.name);
safe = true;
m_Player.PlayerIsSafe.Send(safe);
}
}
/*void OnTriggerStay(Collider other)
{
if (player == null)
{
player = other.transform.parent.GetComponent<Player>(); // Player is in Devdog.General;
}
if (p != null)
{
print("Still colliding with trigger object PLAYER " + p.name);
safe = true;
m_Player.PlayerIsSafe.Send(safe);
}
}*/
void OnTriggerExit(Collider other)
{
if (player == null)
{
player = other.transform.parent.GetComponent<Player>(); // Player is in Devdog.General;
}
if (player != null)
{
print(gameObject.name + " and trigger object PLAYER " + player.name + " are no longer colliding");
safe = false;
m_Player.PlayerIsSafe.Send(safe);
}
}
}