• 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 polarfuchs23 · Mar 03, 2021 at 10:39 PM · gameobjectspriterenderer

Why is gameObject == null?

Hi, I'm facing a problem with my code. I'm calling a function from another script. However when I try to use gameObject it is null. I tried to make a public Sprite Renderer and set a reference in Unity and by code in the Start method. But it returns null as well. Can you help me here?

 public class ChangeSprite : MonoBehaviour
 {
     public Sprite[] spriteArray;
 
     public SpriteRenderer spriteRenderer;
 
     private void Start()
     {
         Debug.Log("Started");
         spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
         Debug.Log(spriteRenderer);
     }
 
     public void changeSprite(int diceNumber)
     {
         Debug.Log(gameObject == null);
         spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
         Debug.Log(spriteRenderer == null);
         spriteRenderer.sprite = spriteArray[diceNumber];
     }
 }

I'm calling changeSprite from another script. Thanks in advance

Comment
Add comment · Show 5
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 unity_ek98vnTRplGj8Q · Mar 03, 2021 at 11:17 PM 0
Share

How are you calling this from the other script? Where does your other script get the reference to this ChangeSprite script? If gameObject is null then that means that you are getting some weird detached version of this script.

avatar image polarfuchs23 · Mar 04, 2021 at 09:07 AM 0
Share

I already changed my code around the problem. I think I called it like this: ChangeSprite changeSecSprite = new ChangeSprite(); int rolledNummber = rnd.Next(1,7); changeSecSprite.changeSprite(rolledNumber

Show more comments
avatar image polarfuchs23 · Mar 04, 2021 at 10:43 AM 0
Share

The script is already attached to a game object. So I should make a reference like

changeSecSprite = Game object.Find("Game object the change script is attached to").GetComponent<ChangeSprite>();

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by polarfuchs23 · Mar 04, 2021 at 12:48 PM

Edit: Ermiq found the mistake. I created a new instance of ChangeSprite in the script I called the function in. ( ChangeScript changeSecSprite = new ChangeScript; ) By this I only called the function changeSprite and skipped the start and update method. The fix is to grab the script via the game object like:

 ScriptName scriptName = GameObject.Find("GameobjectItsAttachedTo").GetComponent<ScriptName>();

So in my case:

 ChangeScript changeSecSprite = GameObject.Find("Dice").GetComponent<ChangeScript>();

Or by instantly calling the function from this:

 GameObject.Find("Dice").GetComponent<ChangeScript>().changeSprite(rolledNumber);

Thanks a lot!!

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

177 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why I cant use this? 1 Answer

How to deal with multiple sprites on a single object without resorting to creating multiple gameobjects 0 Answers

Hide/Show GameObject from script 2 Answers

Which is better performance-wise? 1 Answer

UI Button Sizing unit 0 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