2016-11-18 37 views
-1

我怎麼能實例化對象,在2d,從位置110向下?Unity如何從軸y = 260到10實例化預製件?

我還在尋找,但我無法找到任何,所以我把我的問題放在這裏,希望有人能幫助我。

EmployeeList.cs

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

public class EmployeeList : MonoBehaviour { 

    public GameObject EmployeeTab; 

    // Use this for initialization 
    void Start() 
    { 
     List<Employee> employees = new List<Employee>(); 

     // Create a parent of your instantiated objects 
     GameObject parent = GameObject.Find("Recruit_Employee"); 

     // Position of the instantiated objects 
     Vector3 position = Vector3.up * 110f; 

     // Distance between instantiated objects 
     float step = 50; 

     employees.Add(new Employee("David", 5, 5000)); 
     employees.Add(new Employee("Jason", 10, 10000)); 
     employees.Add(new Employee("Donald", 3, 3000)); 



     foreach (Employee worker in employees) 
     { 
      // Instantiate the objects from a given prefab 
      GameObject w = (GameObject)Instantiate(EmployeeTab, position, Quaternion.identity, parent.transform); 
      w.SetActive(true); 
      // Set the desired name 
      w.name = worker.name; 

      // Don't forget to change the position of the next employee 
      position.y -= step; 

      Debug.Log("Name: " + worker.name + "Skill: " + worker.skill + "Cost: " + worker.cost); 
     } 
    } 

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

    } 
} 

,我想放在一個實例化的foreach部分,所以如果我有2名員工則2個按鍵或組合屋必須創建我稱之爲EmployeeTab

進出口新的中間腳本:/

Employee.cs:

using UnityEngine; 
using System.Collections; 
using System; 

public class Employee : MonoBehaviour 
{ 
    public string name; 
    public int skill; 
    public int cost; 

    public Employee(string newName, int newSkill, int newCost) 
    { 
     name = newName; 
     skill = newSkill; 
     cost = newCost; 
    } 
} 
+0

你嘗試過什麼初始化它那麼遠?另外,你想要實例化哪個對象?一個文本字段? –

回答

0

一旦你知道how to instantiate,這是小菜一碟! )

void Start() 
{ 
    List<Employee> employees = new List<Employee>(); 

    // Create a parent of your instantiated objects 
    GameObject parent = new GameObject("EmployeesParent"); 

    // Position of the instantiated objects 
    Vector3 position = Vector3.up * 110.0f ; 

    // Distance between instantiated objects 
    float step = 10 ; 

    employees.Add(new Employee("David", 5, 5000)); 
    employees.Add(new Employee("Jason", 10, 10000)); 
    employees.Add(new Employee("Donald", 3, 3000)); 

    foreach (Employee worker in employees) 
    { 
     costInfo.text = "Cost: " + CurrencyConverter.Instance.GetCurrencyIntoString(Employee.Cost, false, false, false); 
     EmployeeName.text = Employee.Name; 
     SkillInfo.text = Employee.Skill.ToString(); 

     // Instantiate the objects from a given prefab 
     GameObject w = (GameObject) Instantiate(myPrefab, position, Quaternion.identity, parent.transform); 

     // Set the desired name 
     w.name = worker.name ; 

     // Don't forget to change the position of the next employee 
     position.y -= step ; 
    } 
} 

編輯:MonoBehaviour無法實例像traditionnal C#類。由於您的Employee類似乎是一個簡單的datacontainer,你必須改變你Employee類和啓動功能我寫道:

using UnityEngine; 
using System.Collections; 
using System; 

[System.Serializable] 
public class Employee 
{ 
    public string name; 
    public int skill; 
    public int cost; 
} 

// YOUR OTHER SCRIPT 

// Set the values from the inspector here 
public Employee[] employees ; 

void Start() 
{ 
    // Create a parent of your instantiated objects 
    GameObject parent = new GameObject("EmployeesParent"); 

    // Position of the instantiated objects 
    Vector3 position = Vector3.up * 110.0f ; 

    // Distance between instantiated objects 
    float step = 10 ; 

    for(int i = 0 ; i < employees.Length ; ++i) 
    { 
     costInfo.text = "Cost: " + CurrencyConverter.Instance.GetCurrencyIntoString(employees[i].cost, false, false, false); 
     EmployeeName.text = Employee.Name; 
     SkillInfo.text = Employee.Skill.ToString(); 

     // Instantiate the objects from a given prefab 
     GameObject w = (GameObject) Instantiate(myEmployeePrefab, position, Quaternion.identity, parent.transform); 

     // Set the desired name 
     w.name = worker.name ; 

     // Add the component you want to the instantiated GameObject to show the information of your employee like a UnityEngine.UI.Text component or whatever you want 

     // Don't forget to change the position of the next employee 
     position.y -= step ; 
    } 
} 
+0

我沒有創建任何東西,我得到一個錯誤,說NullReferenceException。 – Kalip

+0

用我的Employee.cs編輯 – Kalip

+0

@Kalip是否爲'myPrefab'製作了一個公共字段並將其設置在檢查器中? –