In my open world game, npcs should have all different look. There are lists with cosmetics Transforms. But when trying to add them to the List of Transforms named "style", there is exception:
NullReferenceException: Object reference not set to an instance of an object. PeopleStyle.Start () (at Assets/Scripts/PeopleStyle.cs:15)
PeopleStyle.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PeopleStyle : MonoBehaviour {
public List<Transform> heads;
public List<Transform> bodys;
public List<Transform> arms;
public List<Transform> legs;
public List<Transform> shoes;
private List<Transform> style;
private void Start() {
style.Add(heads[Random.Range(0, heads.Count)]);
style.Add(bodys[Random.Range(0, bodys.Count)]);
style.Add(arms[Random.Range(0, arms.Count)]);
style.Add(legs[Random.Range(0, legs.Count)]);
style.Add(shoes[Random.Range(0, shoes.Count)]);
foreach (Transform item in heads) {
GameObject obj = Instantiate(item.gameObject, transform.position, transform.rotation) as GameObject;
obj.transform.localScale = GameObject.FindWithTag("ScaleExample").transform.localScale;
obj.transform.parent = this.transform;
}
}
}
FIX:
I did not assign variable style. Would not post this if I sow it, but am working 13 hrs a day on this project.
private List<Transform> style = new List<Transform>();