Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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#并从另一个脚本访问_C#_Unity3d_Unity5 - Fatal编程技术网

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

生成障碍c#并从另一个脚本访问,c#,unity3d,unity5,C#,Unity3d,Unity5,我尝试在manager类中实例化时出错。说 错误CS1061:类型UnityEngine.Object'不包含GetComponent'的定义,并且找不到类型UnityEngine.Object'的扩展方法GetComponent'。是否缺少程序集引用?(CS1061)(汇编CSharp)将此添加到您的障碍物中课程: void Start() { manager = GameObject.FindWithTag("ObstacleManager").GetComponent<Obs

我尝试在manager类中实例化时出错。说
错误CS1061:类型<代码>UnityEngine.Object'不包含<代码>GetComponent'的定义,并且找不到类型<代码>UnityEngine.Object'的扩展方法<代码>GetComponent'。是否缺少程序集引用?(CS1061)(汇编CSharp)

将此添加到您的
障碍物中
课程:

void Start()
{
    manager = GameObject.FindWithTag("ObstacleManager").GetComponent<ObstacleManager>();
}
并在
障碍物中
添加

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;
        }
    }
}
public int noofbsacles;
公众持股比例[]X%;
公共游戏对象[]类型的障碍;
浮动y;
//跟踪哪些繁殖点是空闲的,哪些不使用这些列表
私人名单自由职位;
私人列表占用位置;
私有void Start()
{
自由位置=新列表(点);
占用位置=新列表();
产卵障碍();
}
私密的
{
//只需将其用于初始障碍
//根据需要随时调用Spawn
for(int i=0;ifor(int j=0;j有括号问题!我的错


障碍物obs=((游戏对象)实例化(障碍物类型[j],自由位置[pointsIndex]。位置,四元数.identity)).GetComponent();

我真的很困惑..你能解释更多吗…?:(是否还有使用findWithtag的另一种方法我真的不想使用它…你需要创建对另一个类的引用。因为你的障碍是在运行时生成的,所以你不能在编辑器中填充该变量。你可以使用find,或者实际上你也可以在生成过程中设置它。我编辑了我的答案。这应该让你开始了解如何获取该变量。)e连接。我不确定你是否可以直接在
实例化
上抓取
障碍物obs
,或者你是否需要游戏对象并在另一行中执行此操作。对于延迟的繁殖,你可以执行类似
调用(“繁殖”,5)的操作
。示例中的实例化已简化。只需按需要启动即可。对不起,我太粗鲁了。完成。
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;
        }
    }
}