生成障碍 c# 并从另一个脚本访问

spawn obstacles c# and accessing from another script

我尝试在管理器中实例化时遇到错误 class。说 错误 CS1061:键入 UnityEngine.Object' does not contain a definition forGetComponent' 并且找不到扩展方法 GetComponent' of typeUnityEngine.Object'。您是否缺少程序集参考? (CS1061) (程序集-CSharp)

将此添加到您的 Obstacle class:

void Start()
{
    manager = GameObject.FindWithTag("ObstacleManager").GetComponent<ObstacleManager>();
}

标签显然必须是管理器附加到的游戏对象的标签。

另外:始终以大写字母开头 class 名称(我在代码片段中这样做了,记住这一点,你会得到一个正确的错误)。

也许你真的想稍微改变一下你的产卵。有两个列表,一个用于免费生成点,一个用于占用。当你摧毁障碍物时,将位置传递给产卵函数以将位置移动到空闲列表。

编辑:

创建引用的另一个选项是在生成时在 ObstacleManager 中设置它。为此,您需要获取对实例化障碍的引用。我相信这应该可以在不实际抓住障碍游戏对象的情况下工作,但你也可以这样做。

Obstacle obs = ((GameObject)Instantiate(TypeOfObstacles[j], pointsAvailiable[pointsIndex].position, Quaternion.identity)).GetComponent<Obstacle>();
obs.SetManagerReference(this);

并在Obstacle中添加

public void SetManagerReference(ObstacleManager obsManager)
{
    manager = obsManager;
}

对于空闲位置,您可以这样做:

// in Obstacle.cs
public void OnMouseDown()
{
    manager.SpawnNewObstacle(transform.position);    // you might be able to actually pass the transform, but I'm not sure if it will get destroyed before used in the other function
    Destroy(gameObject);
}

在管理器中:

public  int noOfObsacles;

public float[] xPercent;
public GameObject[] TypeOfObstacles;

float y;

// to keep track of which spawn points are free and which aren't use these lists
private List<Transform> freePositions;
private List<Transform> occupiedPositions;

private void Start()
{
    freePositions = new List<Transform>(spawnPoints);
    occupiedPositions = new List<Transform>();

    SpawnObstacles();
}

private void SpawnObstacles()
{
    // just use this for initial obstacles
    // call Spawn as often as needed
    for(int i = 0; i < noOfObstacles; i++)
    {
        Spawn();
    }
}

// you call this function from the obstacle that gets destroyed
public void SpawnNewObstacle(Vector3 freePos)
{
    // find the spawnpoint in the occupied points
    // and move it to the free ones since the obstacle got destroyed
    for(int i = 0; i < occupiedPositions.Count; i++)
    {
        if(occupiedPositions[i].position == freePos)
        {
            freePositions.Add(occupiedPositions[i]);
            occupiedPositions.RemoveAt(i);
            break;
        }
    }
    // and call Spawn
    Spawn();
}

private void Spawn()
{
    y = Random.value;
    int pointsIndex = Random.Range (0, freePositions.Count); 

    for (int j =0; j<xPercent.Length; j++)
    {

        if ( y <  xPercent[j])
        {
            // these 4 lines are essential for the spawning
            Obstacle obs = ((GameObject)Instantiate(TypeOfObstacles[j], freePositions[pointsIndex], Quaternion.identity).GetComponent<Obstacle>();
            obs.SetManagerReference(this);
            occupiedPositions.Add(freePositions[pointsIndex]);
            freePositions.RemoveAt(pointsIndex);

            break;
        }
    }
}

有括号问题!我的坏

obstacle obs = ((GameObject)Instantiate(TypeOfObstacles[j], freePositions[pointsIndex].position, Quaternion.identity)).GetComponent();