Я думал, что только что закончил приложение, однако при сборке для iOS есть определенный раздел, который возвращает
"NullReferenceException: нулевое значение было найдено, когда экземпляр объектабыло необходимо. "
Вот ошибка Xcode:
NullReferenceException: A null value was found where an object instance was required.
at nextSetAmount.nextButtonCelestials (Boolean onOff) [0x00000] in <filename unknown>:0
at celestialDialogueInstantiator.setActive (Int32 indexToTurnOn) [0x00000] in <filename unknown>:0
at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <filename unknown>:0
at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) [0x00000] in <filename unknown>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchPress (UnityEngine.EventSystems.PointerEventData pointerEvent, Boolean pressed, Boolean released) [0x00000] in <filename unknown>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchEvents () [0x00000] in <filename unknown>:0
at UnityEngine.EventSystems.StandaloneInputModule.Process () [0x00000] in <filename unknown>:0
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
UnityEngine.EventSystems.StandaloneInputModule:Process()
(Filename: currently not available on il2cpp Line: -1)
NullReferenceException: A null value was found where an object instance was required.
at audioManagerCreate.Start () [0x00000] in <filename unknown>:0
и вот соответствующие сценарии, о которых идет речь:
nextSetAmount
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class nextSetAmount : MonoBehaviour {
public GameObject slider;
public GameObject dialogueManager;
public GameObject celestialManager;
public GameObject audioManager;
public GameObject celestialObject;
// Use this for initialization
public void nextButtonDialogue (bool onOff) {
if(onOff == true)
{
dialogueManager.GetComponent<celestialDialogueInstantiator>().makeDialogue(Mathf.RoundToInt(slider.GetComponent<Slider>().value));
}
}
public void nextButtonCelestials (bool onOff) {
if(onOff == true)
{
audioManager.SetActive(true);
AudioListener.pause = true;
for(int i = 0; i < slider.GetComponent<Slider>().value; i++)
{
Transform celestialDialogue = GameObject.Find("celestialDialogue"+i).gameObject.transform;
string celestialName = celestialDialogue.GetChild(2).GetComponent<Text>().text;
float celestialBodyDistance = celestialDialogue.GetChild(4).GetComponent<Slider>().value;
float celestialOrbitalFrequency = celestialDialogue.GetChild(6).GetComponent<Slider>().value;
float celestialRotationalFrequency = celestialDialogue.GetChild(8).GetComponent<Slider>().value;
float celestialBodyDiameter = celestialDialogue.GetChild(10).GetComponent<Slider>().value;
float celestialBodyTemperature = celestialDialogue.GetChild(12).GetComponent<Slider>().value;
// Instantiate celestialObject prefab
var instantiatedCelestial = Instantiate(celestialObject, new Vector3(0,0,0),Quaternion.identity);
// Define properties script for ease of code
var celProps = instantiatedCelestial.GetComponent<celestialProperties>();
// Set name of instantiated prefab
instantiatedCelestial.gameObject.name = ("celestialObject"+i);
// Set parent of instantiated prefab
instantiatedCelestial.transform.SetParent(GameObject.Find("celestialManager").transform);
// Randomise properties of the instantiated celestialObject prefab
celProps.celestialName = celestialName;
celProps.celestialID = i;
// Set distance from centre as i (celestialObject number) + a float value. This ensures that 0 is closest, and x where x = amountOfCelestials is the furthest.
celProps.celestialBodyDistance = celestialBodyDistance;
celProps.celestialOrbitFrequency = celestialOrbitalFrequency;
celProps.celestialRotationalFrequency = celestialRotationalFrequency;
celProps.celestialBodyDiameter = celestialBodyDiameter;
celProps.celestialBodyTemperature = celestialBodyTemperature;
celProps.celestialOrbitAxis = new Vector3(0,1,0);
// Store initial values for these two so that scaling doesn't multiply by itself
celProps.celestialInitOrbitFrequency = celProps.celestialOrbitFrequency;
celProps.celestialInitRotationalFrequency = celProps.celestialRotationalFrequency;
}
}
}
}
celestialDialogueInstantiator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class celestialDialogueInstantiator : MonoBehaviour {
// Define prefab to instantiate
public GameObject celestialDialogue;
public GameObject systemNameCreate;
public GameObject parent;
public int dialogueID;
public GameObject slider;
public GameObject[] celestialObjects;
// Use this for instantiation
public void makeDialogue (int sliderValue)
{
// For loop that instantiates x amount of dialogues where x = argument.
for(int i = 0; i < sliderValue; i++)
{
var instantiatedDialogue = Instantiate(celestialDialogue,transform.position, Quaternion.identity);
instantiatedDialogue.transform.SetParent(GameObject.Find("createDialogue").gameObject.transform, false);
instantiatedDialogue.SetActive(false);
instantiatedDialogue.name = "celestialDialogue"+i;
instantiatedDialogue.GetComponent<dialogueID>().ID = i;
if (i == 0)
{
instantiatedDialogue.SetActive(true);
}
}
}
public void setActive(int indexToTurnOn)
{
// Turn on next dialogue
if(indexToTurnOn != slider.GetComponent<Slider>().value)
{
GameObject.Find("createDialogue").gameObject.transform.GetChild(indexToTurnOn).gameObject.transform.localScale = new Vector3(0,0,0);
GameObject.Find("createDialogue").gameObject.transform.GetChild(1+indexToTurnOn).gameObject.SetActive(true);
}
// When the last next button is pressed.
else
{
// Set active createCelestials GUI panel
Resources.FindObjectsOfTypeAll<GameObject>().FirstOrDefault(g=>g.CompareTag("Finish")).gameObject.SetActive(true);
GameObject.Find("System Title").gameObject.GetComponent<Text>().text = GameObject.Find("dialogueManager").GetComponent<celestialDialogueInstantiator>().systemNameCreate.GetComponent<Text>().text;
this.gameObject.GetComponent<nextSetAmount>().nextButtonCelestials(true);
AudioListener.pause = false;
setAllInactive(parent.transform,true);
}
}
// Sets all inactive if value = true
public void setAllInactive (Transform transform, bool value)
{
foreach (Transform child in transform)
{
if(value == true)
{
child.gameObject.SetActive(!value);
}
}
}
}
audioManagerCreate
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
public class audioManagerCreate : MonoBehaviour {
public AudioMixer systemMixer;
public Slider slider;
// Use this for initialization
void Start ()
{
// Find amount of celestials in scene
// For each celestial, assign corresponding audio mixer group based on celestialID/i (essentially the same value)
for(int i = 0; i < slider.value; i++)
{
GameObject celestialObject = GameObject.Find("celestialObject"+i).gameObject;
string masterMix = "Master";
celestialObject.GetComponent<AudioSource>().outputAudioMixerGroup = systemMixer.FindMatchingGroups(masterMix)[i+1];
}
}
}
Я не лучший вустранение неполадок, но я предполагаю, что проблема существует, потому что он не может найти gameObject, на который указывает GameObject.Find ().Он прекрасно работает в Unity и находит объекты без ошибок.
Единственная константа, которую я вижу, это то, что я пытаюсь найти объект по его имени + i, где i - идентификатор цикла for-int.Мне нужно сделать это, потому что он ищет объекты, которые не существуют, пока они не будут созданы, и имена которых основаны на их идентификаторе из цикла for-int в их сценарии создания экземпляров.