我以为我已经完成了一个应用程序,但是当我为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.Find()指向的gameObject。它在Unity中完全可以正常工作,并且可以毫无故障地找到对象。
我能看到的唯一常量是我试图根据其名称+ i查找对象,其中i是for-int循环ID。我之所以需要这样做,是因为它会查找在实例化之前不存在的对象,并且谁的名称基于其实例化脚本中for-int循环的ID。