2016-11-18 3 views
-1

Как я могу создавать объекты в 2d, от pos y 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() { 

    } 
} 

И я хочу поставить в Instantiate в разделе foreach, так что если у меня есть 2 сотрудников, то 2 кнопки или префабы должен быть создан, который я называю EmployeeTab .

Im новой для промежуточного сценариев:/

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 ; 
    } 
} 

EDIT: MonoBehaviour не может быть реализован как класс C# традиционных французской. Так как ваш класс Employee, кажется, простой datacontainer, вы должны изменить свой класс Employee и функция Start I писал:

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' и установили его в инспекторе? –

Смежные вопросы