Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity:如何避免将游戏对象拖到公共脚本变量中?_C#_Unity3d_Gameobject - Fatal编程技术网

C# Unity:如何避免将游戏对象拖到公共脚本变量中?

C# Unity:如何避免将游戏对象拖到公共脚本变量中?,c#,unity3d,gameobject,C#,Unity3d,Gameobject,我有一个名为“玩家”的游戏对象和一个名为“火力点”的游戏对象: 此外,我还写了一个非常基本的c#脚本“武器”: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Weapon : MonoBehaviour { public Transform firePoint; // Start is called before the first f

我有一个名为“玩家”的游戏对象和一个名为“火力点”的游戏对象:

此外,我还写了一个非常基本的c#脚本“武器”:

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

public class Weapon : MonoBehaviour
{

    public Transform firePoint;

    // Start is called before the first frame update
    void Start()
    {

    }

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

    }
}
我在玩家游戏对象上拖动了脚本,现在它显示在检查器中。在脚本中,我想使用FirePoint,即在Player中。我所知道的唯一方法是将壁炉点拖到位置保持器中:


我想避免手动拖放,因为我想在游戏中产生拥有自己火力点的新玩家。如何以编程方式实现这一点?我找不到任何教程(可能是因为我不知道如何搜索它),所以我也很高兴能有一个链接到一个简单的教程来解决这个问题。

您可能会看到使用预置和

Unity的预制系统允许您创建、配置和存储游戏对象,包括其所有组件、属性值和子游戏对象 作为可重用的资产。预设资源用作模板,从中可以在场景中创建新的预设实例


预置允许您将游戏对象构建为模板,并在运行时创建其克隆。当实例化一个对象时,它发生在代码中,并且可以在代码本身中分配。你可以在你的玩家预置中分配一次火力点,任何时候你实例化或克隆预置时,它都会创建并分配自己的火力点。

你可能会考虑使用预置和

Unity的预制系统允许您创建、配置和存储游戏对象,包括其所有组件、属性值和子游戏对象 作为可重用的资产。预设资源用作模板,从中可以在场景中创建新的预设实例


预置允许您将游戏对象构建为模板,并在运行时创建其克隆。当实例化一个对象时,它发生在代码中,并且可以在代码本身中分配。你可以在你的玩家预置中分配一次火力点,任何时候你实例化或克隆预置时,它都会创建并分配自己的火力点。

除了已经建议的预置之外

一般编程中的“正确”方法是:在firePoint对象上创建一个类,如

// The class doesn't have to do anything, it is just for identification
public class FirePoint : MonoBehaviour { }
然后使用

public class Weapon : MonoBehaviour
{
    public FirePoint firePoint;
}
这将强制引用的游戏对象具有
火点
组件,并确保您不会在此处意外引用其他内容

现在,您还可以在运行时简单地获取它,以防您忘记/不想手动引用它

private void Awake ()
{
    if(!firePoint) firePoint = GetComponentInChildren<FirePoint>(true);
}
private void Awake()
{
如果(!firePoint)firePoint=getComponentChildren(true);
}

除了已经建议的预制之外

一般编程中的“正确”方法是:在firePoint对象上创建一个类,如

// The class doesn't have to do anything, it is just for identification
public class FirePoint : MonoBehaviour { }
然后使用

public class Weapon : MonoBehaviour
{
    public FirePoint firePoint;
}
这将强制引用的游戏对象具有
火点
组件,并确保您不会在此处意外引用其他内容

现在,您还可以在运行时简单地获取它,以防您忘记/不想手动引用它

private void Awake ()
{
    if(!firePoint) firePoint = GetComponentInChildren<FirePoint>(true);
}
private void Awake()
{
如果(!firePoint)firePoint=getComponentChildren(true);
}