我正在做游戏,我有一个带有开始按钮的主菜单,该按钮工作得很好。唯一的问题是您可以看到背景,所以我添加了另一个名为“ fadeImage”的图像,因此我想拥有它,因此只要负责检测单击按钮的功能,淡出名为“ fadeImage”的图像。我尝试使用GetComponent和GetGameObject,每当我使用CrossFade Alpha时,在第51行都会收到错误Object reference not set to instance of object
。
这是我的代码
using UnityEngine;
using System.Collections;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class StartOptions : MonoBehaviour
{
public int sceneToStart = 1; //Index number in build settings of scene to load if changeScenes is true
public bool changeScenes; //If true, load a new scene when Start is pressed, if false, fade out UI and continue in single scene
public bool changeMusicOnStart; //Choose whether to continue playing menu music or start a new music clip
public Image fadeImage;
[HideInInspector] public bool inMainMenu = true; //If true, pause button disabled in main menu (Cancel in input manager, default escape key)
[HideInInspector] public Animator animColorFade; //Reference to animator which will fade to and from black when starting game.
[HideInInspector] public Animator animMenuAlpha; //Reference to animator that will fade out alpha of MenuPanel canvas group
public AnimationClip fadeColorAnimationClip; //Animation clip fading to color (black default) when changing scenes
[HideInInspector] public AnimationClip fadeAlphaAnimationClip; //Animation clip fading out UI elements alpha
private PlayMusic playMusic; //Reference to PlayMusic script
private float fastFadeIn = .01f; //Very short fade time (10 milliseconds) to start playing music immediately without a click/glitch
private ShowPanels showPanels; //Reference to ShowPanels script on UI GameObject, to show and hide panels
void Awake()
{
//Get a reference to ShowPanels attached to UI object
showPanels = GetComponent<ShowPanels> ();
//Get a reference to PlayMusic attached to UI object
playMusic = GetComponent<PlayMusic> ();
fadeImage = GetComponent<Image>();
}
public void StartButtonClicked()
{
//If changeMusicOnStart is true, fade out volume of music group of AudioMixer by calling FadeDown function of PlayMusic, using length of fadeColorAnimationClip as time.
//To change fade time, change length of animation "FadeToColor"
fadeImage.CrossFadeAlpha(1.0f, 1.0f, true);
if (changeMusicOnStart)
{
playMusic.FadeDown(fadeColorAnimationClip.length);
}
//If changeScenes is true, start fading and change scenes halfway through animation when screen is blocked by FadeImage
if (changeScenes)
{
//Use invoke to delay calling of LoadDelayed by half the length of fadeColorAnimationClip
Invoke ("LoadDelayed", fadeColorAnimationClip.length * .5f);
//Set the trigger of Animator animColorFade to start transition to the FadeToOpaque state.
animColorFade.SetTrigger ("fade");
}
//If changeScenes is false, call StartGameInScene
else
{
//Call the StartGameInScene function to start game without loading a new scene.
StartGameInScene();
}
}
void OnEnable()
{
SceneManager.sceneLoaded += SceneWasLoaded;
}
void OnDisable()
{
SceneManager.sceneLoaded -= SceneWasLoaded;
}
//Once the level has loaded, check if we want to call PlayLevelMusic
void SceneWasLoaded(Scene scene, LoadSceneMode mode)
{
//if changeMusicOnStart is true, call the PlayLevelMusic function of playMusic
if (changeMusicOnStart)
{
playMusic.PlayLevelMusic ();
}
}
public void LoadDelayed()
{
//Pause button now works if escape is pressed since we are no longer in Main menu.
inMainMenu = false;
//Hide the main menu UI element
showPanels.HideMenu ();
//Load the selected scene, by scene index number in build settings
SceneManager.LoadScene (sceneToStart);
}
public void HideDelayed()
{
//Hide the main menu UI element after fading out menu for start game in scene
showPanels.HideMenu();
}
public void StartGameInScene()
{
//Pause button now works if escape is pressed since we are no longer in Main menu.
inMainMenu = false;
//If changeMusicOnStart is true, fade out volume of music group of AudioMixer by calling FadeDown function of PlayMusic, using length of fadeColorAnimationClip as time.
//To change fade time, change length of animation "FadeToColor"
if (changeMusicOnStart)
{
//Wait until game has started, then play new music
Invoke ("PlayNewMusic", fadeAlphaAnimationClip.length);
}
//Set trigger for animator to start animation fading out Menu UI
animMenuAlpha.SetTrigger ("fade");
Invoke("HideDelayed", fadeAlphaAnimationClip.length);
Debug.Log ("Game started in same scene! Put your game starting stuff here.");
}
public void PlayNewMusic()
{
//Fade up music nearly instantly without a click
playMusic.FadeUp (fastFadeIn);
//Play music clip assigned to mainMusic in PlayMusic script
playMusic.PlaySelectedMusic (1);
}
}
无论何时我删除任何引用该图像的代码,“开始”按钮都将起作用,但是当我没有收到该错误时,“开始”按钮将保持蓝色,这应该是在鼠标悬停在其上方时发生的。
答案 0 :(得分:0)
fadeImage = GetComponent<Image>();
函数中的这一行Start
仅在将此脚本附加到图像时才有效。这就是为什么出现空引用错误的原因,因为具有这种单行为的对象没有图像组件。
答案 1 :(得分:0)
似乎
fadeImage = GetComponent<Image>();
仅会失败并返回null
,因为此脚本所附加的Image
上没有GameObject
组件。
GetComponent如果游戏对象附加了一个,则返回Type类型的组件,否则返回null。
你说
我在按钮中添加了一个名为fadeImage的图像
所以我想说,您宁愿使用GetComponentInChildren
在该脚本所附的Image
下的下的层次结构中查找GameObject
递归组件
// By passing a true as parameter you make also
// Sure that the component will also be found
// if it is currently not active or disabled - otherwise it would be ignored
fadeImage = GetComponentInChildren<Image>(true);