触发时未使用Sprite Renderer出现对象 [英] Object doesn't appear using Sprite Renderer when triggered

查看:560
本文介绍了触发时未使用Sprite Renderer出现对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的作业统一制作一个Mario副本,并且尝试制作不可见"块,它从不可见开始,然后在被击中时变为可见.我正在尝试使用SpriteRenderer.enable使其工作,它可以在一开始就将其关闭,但在尝试使其可见时则不能.

I'm making a Mario replica in unity for my homework, and I'm trying to make the "Invisible" block, it starts off invisible, then when hit, it turns visible. I'm trying to use SpriteRenderer.enable to make it work, it works for turning it off in the start, but not when trying to make it visible.

我已经尝试为这个特定的块创建一个单独的脚本,但是结果是相同的.所有标记均已正确设置,我尝试使用Debug.log来查看是否在启用了Sprite的位置输入了"if",但结果是否定的.

I've tried to create a separate script for this particular block, but results are the same. All the tags are set correctly, I've tried using Debug.log to see if I enter the "if" where the sprite should be enabled, but the result is negative.

这是一个关闭特定块的精灵渲染器的启动方法(起作用):

This is the start method turning off the sprite renderer for the particular block (it works):

private void Start()
{
    //rendObject = this.gameObject.GetComponent<SpriteRenderer>();
    if (gameObject.tag == "Invisible")
    {
        gameObject.GetComponent<SpriteRenderer>().enabled = false;
    }
}

这是所有块脚本:

private void OnCollisionEnter2D(Collision2D collision)
{
    if (timesToBeHit > 0)
    {
        if (collision.gameObject.tag == "Player" && IsPlayerBelow(collision.gameObject))
        {
            if (gameObject.tag == "Invisible")
            {
                gameObject.GetComponent<SpriteRenderer>().enabled = true;
            }
            collision.gameObject.GetComponent<PlayerController>().isJumping = false; //Mario can't jump higher
            Instantiate(prefabToAppear, transform.parent.transform.position, Quaternion.identity); //instantiate other obj
            timesToBeHit--;
            anim.SetTrigger("GotHit"); //hit animation   
        }
    }

    if (timesToBeHit == 0)
    {
        anim.SetBool("EmptyBlock", true); //change sprite in animator
    }
}

推荐答案

我们已经在聊天中找到了解决方案,但是对于所有可能会遇到这种问题的人,都需要检查以下内容:

We've found a solution in chat, but for all people who may run or have run on this kind of problem will need to check for the next things:

  • 必须具有2个任何类型的碰撞体,每个gameObject 1个.
  • 至少1个僵尸.
  • 适当设置对撞机.
  • 适当的标签.
  • 适当的层碰撞矩阵.

下面的代码将起作用.

public SpriteRenderer render;

void Start()
{
    render.enabled = false;
}

private void OnCollisionEnter2D(Collision2D other)
{
    if (other.gameObject.tag == "Player")
    {
        render.enabled = true;
    }
}

这篇关于触发时未使用Sprite Renderer出现对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆