从其他脚本中设置主动

发布于 2025-02-05 06:26:39 字数 864 浏览 1 评论 0原文

我需要在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长安忆 2025-02-12 06:26:39

在Unity答案上,有很多答案,例如,这里是一个答案问题

如果您的Playertats是一类,则将其视为布尔值。您将获得对脚本组件的引用,然后立即将该值设置为true。

假设您的游戏巨星类中有一个公共布尔激活,您可以做这样的事情:

PlayerStats playerStats = player.GetComponent<PlayerStats>();
playerStats.activate = 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:

PlayerStats playerStats = player.GetComponent<PlayerStats>();
playerStats.activate = true;

This is based on pure assumption of your PlayerStats class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文