简体   繁体   English

gameObject SetActive(false) 正在禁用我的所有子对象

[英]gameObject SetActive(false) is disabling all my child objects

I have two scripts a parent and a child.我有两个脚本一个父母和一个孩子。 The parent script provides the On Mouseover method for the child to use if Input.("Fire1") is used.如果使用 Input.("Fire1"),父脚本会提供 On Mouseover 方法供子级使用。 and then it SHOULD disable the collider the mouse is over and score a point.然后它应该禁用鼠标经过的对撞机并得分。 But I am having an error where if I click on one prefab collider with the child script attached, it will then disable all prefab game objects.但是我遇到了一个错误,如果我单击一个带有子脚本的预制对撞机,它将禁用所有预制游戏对象。 how can I fix this so my child script only allows for one disabling of a prefab at a time?我该如何解决这个问题,以便我的子脚本一次只允许禁用一个预制件?

Parent Script父脚本

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

public class CollectableParent : MonoBehaviour
{
    
    protected GameManager gameManager;
    // Start is called before the first frame update
    void Awake()
    {
        gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnMouseOver()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            this.gameObject.SetActive(false);
            gameManager.UpdateScore(5);
        }
    }

 
    

    
    
}

ChildScript童书

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

public class LevelOneJack : CollectableParent
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetButtonDown("Fire1"))
        {
            OnMouseOver();
           
        }
        
    }
   


}

so i removed the the child class if statement from the child class "Update" method and am instead calling the OnMoseOver method, in a override method.所以我从子 class“更新”方法中删除了子 class if 语句,而是在覆盖方法中调用 OnMoseOver 方法。 which works well, the objects now only get disabled one at a time.效果很好,对象现在一次只能禁用一个。 I figure the problem is that I was disabling the parent script there for disabling all children scripts and objects too.我认为问题是我在那里禁用了父脚本以禁用所有子脚本和对象。

the only issue I have now is, when I hover the moues over an object, it automatically fires the UpdateScore method, even though there is an If statement checking if Input is true before it does the action.我现在唯一的问题是,当我 hover 鼠标悬停在 object 上时,它会自动触发 UpdateScore 方法,即使在执行操作之前有一个 If 语句检查 Input 是否为真。 Its because i had the if statement syntax wrong这是因为我的 if 语句语法错误

This is the updated child script这是更新的子脚本

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LevelOneJack : CollectableParent
 {
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
       
     }
 
         public override void OnMouseOver()
          {
         if(Input.GetButtonDown("Fire1"))
             base.OnMouseOver();
             gameManager.UpdateScore(5);
          }
 
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM