Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cobrryse · Mar 15, 2014 at 08:22 PM · 2dspriterenderer

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);
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
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");
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cobrryse · Mar 15, 2014 at 08:52 PM 0
Share

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)

avatar image fafase · Mar 15, 2014 at 09:04 PM 0
Share

Do you have an object called "Hat1"?

avatar image Cobrryse · Mar 15, 2014 at 09:18 PM 0
Share

Yes alt text

untitled.png (3.5 kB)
avatar image Cobrryse · Mar 15, 2014 at 09:59 PM 0
Share

Anybody out there?

avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cobrryse · Mar 15, 2014 at 10:11 PM 0
Share

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)

avatar image Cobrryse · Mar 15, 2014 at 10:13 PM 0
Share

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...

avatar image Cobrryse · Mar 15, 2014 at 11:05 PM 0
Share

Alright I will try this :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

hide animated Sprite by code (SpriteRenderer) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges