【问题标题】:Instantiated prefab is not working as expected but when the prefab is put on the scene then everything runs as expected实例化的预制件没有按预期工作,但是当预制件放在现场时,一切都按预期运行
【发布时间】:2020-08-06 00:56:29
【问题描述】:

我有一个附加到预制件的脚本,脚本是:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class destroyer : MonoBehaviour
{
    
    Circles circles;

    CircleCollider2D collider1;
    Collider2D collider2;


    private void Start()
    {


        circles = FindObjectOfType<Circles>();

    }
 
    private void OnEnable()
    {
        collider1 = gameObject.transform.GetChild(0).GetComponent<CircleCollider2D>();
        collider2 = gameObject.transform.GetChild(1).GetComponent<Collider2D>();
    }

    private void Update()
    {
        if (transform.position.y < 2)
        {
            Destroy(gameObject);
            circles.instantiator();
            
        }
      



    }
    void OnTriggerStay2D(Collider2D other)
    {
      

        if (collider1.bounds.Contains(other.bounds.max) && collider1.bounds.Contains(other.bounds.min))
       {
            
            if (other.bounds.Contains(collider2.bounds.max) && other.bounds.Contains(collider2.bounds.min))
            {
                
                if (transform.position.y > 3)
                {
                    
                    Destroy(other.gameObject);
                    Destroy(gameObject);
                    circles.instantiator();
                }

            }

        }
    }





}

当我实例化预制件时,即使条件为真,if 条件也不会运行。 但是当我将预制件放在场景中并玩游戏时,如果条件根据条件运行良好。我无法弄清楚这是什么问题。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您是否按顺序添加了预制件的子代。带有 2D 圆形碰撞器的 GameObject 应该是第一个,第二个孩子应该有一个 2D 碰撞器。

    试试:

    collider1 = GetComponentInChildren<CircleCollider2D>();
    collider2 = GetComponentInChildren<Collider2D>();
    

    【讨论】:

    • 我已经找出问题所在。问题不在于碰撞器,而是在实例化预制件时,碰撞器的 z 轴边界为 15.8,因为子对象的 z 轴位置为 15.8,这就是为什么从未满足条件
    猜你喜欢
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多