从其他脚本中设置主动
我需要在我的UI中播放图标。由于我使用拾取脚本,因此一旦拾取对象,我想激活该对象。我在主要脚本中制作了一个布尔,但我无法在我的选择脚本中打开或关闭布尔。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NadePickUp : MonoBehaviour
{
public GameObject PUEffect;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
PickUp(other);
// activate = true;
}
}
void PickUp(Collider2D player)
{
Shooting stats = player.GetComponent<Shooting>();
stats.shotType = "grenade";
GameObject effect = Instantiate(PUEffect, transform.position, Quaternion.identity);
PlayerStats activate = player.GetComponent<PlayerStats>();
activate = true;
Destroy(gameObject);
}
}
I need to active icons in my UI. Since I make use of a pick up script I want to activate the object once I picked it up. I made a bool in my main script but I can't turn the bool on or off in my pick up script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NadePickUp : MonoBehaviour
{
public GameObject PUEffect;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
PickUp(other);
// activate = true;
}
}
void PickUp(Collider2D player)
{
Shooting stats = player.GetComponent<Shooting>();
stats.shotType = "grenade";
GameObject effect = Instantiate(PUEffect, transform.position, Quaternion.identity);
PlayerStats activate = player.GetComponent<PlayerStats>();
activate = true;
Destroy(gameObject);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Unity答案上,有很多答案,例如,这里是一个答案问题。
如果您的
Playertats
是一类,则将其视为布尔值。您将获得对脚本组件的引用,然后立即将该值设置为true。假设您的游戏巨星类中有一个
公共布尔激活
,您可以做这样的事情:这是基于
Playertats
类的纯假设。There are many answers to something like this on Unity Answers, here for example is one that answers your question.
If your
PlayerStats
is a class, you're treating it as a boolean. You're getting a reference to the script component and then immediately setting that value to true.Assuming there's a
public boolean activate
on your PlayerStats class you could do something like this:This is based on pure assumption of your
PlayerStats
class.