如何在实例化指定的预制件后实例化预制件?

时间:2015-11-12 08:00:27

标签: c# unity3d

我有Enemy.cs来随机实例化敌人 然后我想得到一个hero.cs来实例化英雄。

要求是在相应的敌人之后实例化英雄 例如,在场景中找到enemy_01,然后实例化hero_01 在场景中找到enemy_02,然后实例化hero_02 没有enemy_03,不应该实例化hero_03。

更难,我想选择要实例化的英雄 例如,在场景中使用enemy_01,有80%的机会实例化hero_01,有20%的几率实例化hero_02。 在场景中使用enemy_02,有50%几率实例化hero_02,有50%几率实例化hero_03。
但是如果没有enemy_03,则无法实例化hero_03。

你能给我一个想法或想法吗?

My Hero.cs

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

public class Hero_list : MonoBehaviour 
{
    [System.Serializable]
    public class Hero
    {
        public GameObject Hero_prefab;
        public GameObject Need_prefab;
    }
    public List <Hero> Heroes = new List <Hero> ();
    public static Hero_list instance;

    public void findEnemy()
    {
        GameObject[] respawns = GameObject.FindGameObjectsWithTag("Enemy");

        foreach (GameObject respawn in respawns)
        {
            Debug.Log(respawn);

        }
    }


    void Update () 
    {
        findEnemy();

    }
}

1 个答案:

答案 0 :(得分:2)

One possibility is to let the enemy call findEnemy() of Hero.cs when an it is spawned, either in its Start() or OnAwake() method. Then spawn the hero.

Since these are just normal functions, you can execute whatever logic you want in them, thus being able to chain instantiation of game objects.