简体   繁体   English

Unity 2D C# - 育儿问题对我的游戏不起作用

[英]Unity 2D C# - Parenting not working question for my game

So I am trying to make a top-down tycoon game where a truck gives you crates and you sell the crates for money.所以我试图制作一个自上而下的大亨游戏,卡车给你板条箱,你卖板条箱换钱。 I have a script for the trucks and it's working just fine except for the first part.我有一个卡车脚本,除了第一部分外,它工作得很好。 So I coded it that when the trucks are spawned 2 crates spawn and are parented to the truck game object, and the issue is that when I run the game, the objects spawn and all, but they aren't parented to the trucks, I have 6 trucks in the scene and only one gets their crate parented.所以我编码它,当卡车产生时 2 个板条箱产生并且是卡车游戏对象的父级,问题是当我运行游戏时,对象产生等等,但它们不是卡车的父级,我现场有 6 辆卡车,但只有一辆卡车将其板条箱设置为父级。 Can someone help?有人可以帮忙吗?

Code:代码:

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

public class CarScript : MonoBehaviour
{
   private float speed = 0.01f;

   private Vector3 dropoffLocation;

   private bool hasCrates = true;

   private float dropCratesCooldown = 500f;

   GameObject cratess;

   private float crateUpgrade = 1;

   public GameObject crateUpgrade1;
   public GameObject crateUpgrade2;
   public GameObject crateUpgrade3;
   public GameObject crateUpgrade4;

   private void Start()
   {
      dropoffLocation = new Vector3(3.5f, -7f, 0);

      crateUpgrade1 = GameObject.Find("Boxes In Truck 1");
      crateUpgrade2 = GameObject.Find("Boxes In Truck 2");
      crateUpgrade3 = GameObject.Find("Boxes In Truck 3");
      crateUpgrade4 = GameObject.Find("Boxes In Truck max");

      if (crateUpgrade == 1)
      {
         Instantiate(crateUpgrade1);
         crateUpgrade1.transform.parent = transform;
         //crateUpgrade1.transform.position = new Vector3(0, 0, 0);
      }
   }

   // Update is called once per frame
   void Update()
   {
      if (transform.position.x > 40)
      {
         Destroy(gameObject);
      }

      if (transform.position.x > dropoffLocation.x && hasCrates)
      {
         dropCratesCooldown--;

         if (dropCratesCooldown < 0)
         {
            cratess = transform.GetChild(0).gameObject;

            Destroy(cratess);
            hasCrates = false;
         }
         transform.Translate(0, 0, 0);
      }
      else
      {
         transform.Translate(speed, 0, 0);
      }
   }
}

btw unity doesn't even show me that its an error btw unity 甚至没有告诉我这是一个错误

Thanks in advance!提前致谢!

Instantiate returns the instantiated game object. Instantiate返回实例化的游戏对象。 That's what you want to assign as a child of the truck.这就是您要指定为卡车子项的内容。 The way you're doing it now, you just keep assigning and reassigning crateUpgrade1 's parent.按照您现在的做法,您只需继续分配和重新分配crateUpgrade1的父项。 That's the reason that only one of your trucks get a crate parented to it.这就是为什么只有您的一辆卡车配备了一个板条箱。

This is how it should be:应该是这样:

GameObject crate = Instantiate(crateUpgrade1);
crate.transform.parent = transform;

One other thing;另一件事; the standard way to instantiate new objects is to create a prefab asset in the project, then assign a reference to it to CarScript , and then instantiate instances of that prefab.实例化新对象的标准方法是在项目中创建一个预制资产,然后将对其的引用分配给CarScript ,然后实例化该预制的实例。 So the crateUpgrade1 = GameObject.Find("Boxes In Truck 1");所以crateUpgrade1 = GameObject.Find("Boxes In Truck 1"); part is a little weird.部分有点奇怪。

This is how it's usually done这是通常的做法

public class CarScript : MonoBehaviour
{
   // Assign this to your "crate" prefab in the Unity Inspector
   [SerializeField] private GameObject cratePrefab;

   private void Start()
   {
      if (crateUpgrade == 1)
      {
         GameObject crate = Instantiate(this.cratePrefab);
         crate.transform.parent = transform;
      }
   }
}

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

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