动态对象不会加载到场景中

时间:2018-05-14 19:58:55

标签: unity3d

我有车库游戏。我将汽车选择脚本与动态GameObjects汽车一起使用,从资源加载为对象。我还想动态加载轮子,因为我有一套40个轮子。

因此,当游戏开始时,第一辆汽车在场景中初始化,脚本轮产生车轮,将它们附加在车轮容器上。更换汽车后,我采用相同的逻辑,但车轮不再启动。

这是我的车库逻辑。有人可以帮忙吗?

public class GarageController : MonoBehaviour {
public GameObject m_CarsCollectionHolder;
private Object[] cars = null;
private Object[] wheels = null;
private GameObject current_car = null;
private GameObject current_wheel_FL, current_wheel_FR, current_wheel_RL, current_wheel_RR;
private GameObject FL, FR, RL, RR;
private int current_car_id = 0;
private int current_wheels_id = 0;

void Start () {
    int first_time_init = PlayerPrefs.GetInt("first_time_init");
    if(first_time_init == 0)
    {
        this.FirstTimeInit();
    }

    this.LoadWheels();
    //Car load last;
    this.CarInit();
}

/*
 * First time game open init
 */
private void FirstTimeInit()
{
    PlayerPrefs.SetInt("first_time_init", 1);
    PlayerPrefs.SetInt("selected_car_id", 0);
}

/*
 * Car init when game object ( based on last car selection and customizations )
 */
private void CarInit()
{
    this.current_car_id = PlayerPrefs.GetInt("selected_car_id");
    this.cars = Resources.LoadAll("cars", typeof(GameObject));
    this.SetCar(this.current_car_id);
}


/*
 * Car selection controllers
 */
public void NextCar()
{
    if(this.current_car_id + 1 < cars.Length)
    {
        this.current_car_id++;
    }
    else
    {
        this.current_car_id = 0;
    }
    this.SetCar(this.current_car_id);
}

public void PreviousCar()
{
    if (this.current_car_id - 1  >= 0)
    {
        this.current_car_id--;
    }
    else
    {
        this.current_car_id = cars.Length - 1;
    }
    this.SetCar(this.current_car_id);
} 


private void SetCar(int car_id)
{
    Destroy(this.current_car);
    this.current_car = Instantiate(cars[car_id]) as GameObject;
    this.current_car.transform.SetParent(m_CarsCollectionHolder.transform, false);

    // Get current wheels containers
    this.current_wheel_FL = GameObject.Find("FL");
    this.current_wheel_FR = GameObject.Find("FR");
    this.current_wheel_RL = GameObject.Find("RL");
    this.current_wheel_RR = GameObject.Find("RR");

    //Set history wheels
    this.current_wheels_id = PlayerPrefs.GetInt("selected_wheels_for_car_" + car_id);
    this.ChangeWheels(current_wheels_id);

    SetCurrentCarId(car_id);
}

private void SetCurrentCarId(int car_id)
{
    PlayerPrefs.SetInt("selected_car_id", car_id);
}


// Wheels controller
private void LoadWheels()
{
    this.wheels = Resources.LoadAll("wheels", typeof(GameObject));
}

private void ChangeWheels(int current_wheels_id)
{
    //Destroy old wheels
    Destroy(this.FL);
    Destroy(this.FR);
    Destroy(this.RL);
    Destroy(this.RR);

    //Init wheels
    this.FL = Instantiate(this.wheels[current_wheels_id]) as GameObject;
    this.FR = Instantiate(this.wheels[current_wheels_id]) as GameObject;
    this.RL = Instantiate(this.wheels[current_wheels_id]) as GameObject;
    this.RR = Instantiate(this.wheels[current_wheels_id]) as GameObject;

    //Move wheels in their containers, and rotate
    this.FL.transform.SetParent(current_wheel_FL.transform, false);
    this.FR.transform.SetParent(current_wheel_FR.transform, false);
    this.FR.transform.Rotate(0, 180, 0);
    this.RL.transform.SetParent(current_wheel_RL.transform, false);
    this.RR.transform.SetParent(current_wheel_RR.transform, false);
    this.RR.transform.Rotate(0, 180, 0);

}

}

1 个答案:

答案 0 :(得分:1)

他们没有实例化,或者您无法将父母设置到适当的位置? 我认为当你摧毁这个物体时,例如&#34; FL&#34;,你会失去你在&#34; current_wheel_FL&#34;所以你不能使用它的变换&#34;将其设置为父级。