- Home /
Show sprite by code from another scene
So basically I am trying to get hats to display on my character in the main game scene by pressing a button in a menu that branches off of the main menu. Right now when I press the button I get an error with my reference to the partyhat in the main game scene called: "Cannot implicitly convert type UnityEngine.SpriteRenderer' to
UnityEngine.GameObject'". Here's the code and if you want any clarification I'd be happy to help. Thanks ahead of time!
using UnityEngine;
using System.Collections;
public class HatSelectMenu : MonoBehaviour {
private GameObject partyHat;
void Start () {
partyHat = GameObject.Find("Hat1").GetComponent<SpriteRenderer>();
}
void OnGUI () {
if(GUI.Button (new Rect(Screen.width / 2 - 50, Screen.height / 2 - 40, 100, 50), "Show Hat"))
{
if(partyHat.activeSelf)
partyHat.SetActive(true);
if(partyHat.renderer)
partyHat.renderer.enabled = true;
}
if(GUI.Button (new Rect(Screen.width / 2 - 50, Screen.height / 2 + 20, 100, 50), "Main Menu"))
{
Application.LoadLevel(0);
}
}
}
Answer by fafase · Mar 15, 2014 at 08:33 PM
private GameObject partyHat;
void Start () {
partyHat = GameObject.Find("Hat1").GetComponent<SpriteRenderer>();
}
partyHat is meant to be a GameObject but you are trying to assign a SpriteRenderer. This might be what you are after.
private GameObject partyHat;
void Start () {
partyHat = GameObject.Find("Hat1");
}
Sorry, but this doesn't work. It gives me this: NullReferenceException: Object reference not set to an instance of an object HatSelect$$anonymous$$enu.OnGUI () (at Assets/Scripts/HatSelect$$anonymous$$enu.cs:16)
Answer by hamcav · Mar 15, 2014 at 10:01 PM
Hey there,
From unity Script Reference: "If no game object with name can be found, null is returned.[..] This function only returns active gameobjects."
Change your code to what fafase said and activate your gameobject on the scene. Thats why you get a nullreference exception.
Or a different way would be to make your "Hat1" gameobject public and attach it from the editor. This way you wont get a nullreferenceexception too and it can remain deactivated...
Hope this helps
------------------------ EDIT ------------------------------------- Ok tested some stuff and found your solution now.
Now when you switch scenes basically every gameobjects gets destroyed, so even if you have your hat object in scene 1 it doesnt exist anymore in scene 2. Therefore you cant access it and get a null exception.
So you have to do several things now. First go over to your scene where you have your Hat1 gameobject. Create a new script for this call it "StaticHat" (name it whatever you want but remember this name!) and attach the following lines
public static GameObject hat1;
void Start(){
hat1 = GameObject.Find("Hat1");
}
saw the difference? :D yes its static. static means its will exist during application runtime. Thus even if you switch scenes your static variables will not be destroyed instead they remain save.
So go to your next scene now. Create another script.And add the following:
GameObject hat;
void Start(){ hat = StaticHat.hat; //So you can see that this worked Debug.Log(hat.name); }
Good Luck ;D
Sorry but its still not working... do you know I am dealing with two different scenes(The script is in a menu scene and the hat is supposed to be in the main game scene)
I need the hat to be hidden until you click the button in the menu so if I activate the gameobjects spriterenderer it will show from the beginning...
Your answer
Follow this Question
Related Questions
Accessing FilterMode possible on a 2D sprite? 1 Answer
Unity2D: Renderer off/ on by triggers 0 Answers
Does SetPropertyBlock work with new 2D SpriteRenderer? 1 Answer
Sprite Shading 1 Answer