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

avatar image Ermiq polarfuchs23 · Mar 04, 2021 at 09:31 AM 1
Share

Don't create a MonoBehaviour with new. It makes no sense and leads to this kind of situations. The MonoBehaviour instance you create with new has no gameObject, and it won't execute its Start() and Update() methods. Because to have a gameObject and to execute Start() and other MonoBehaviour methods the MonoBehaviour must be attached to some GameObject.
To get this script working you need to attach it to the object which you want to change. Either manually attach it in the editor, or - if you have these objects created dynamically at runtime - you'll need to add the script to them right after their creation with myNewSpriteObject.gameObject.AddComponent<ChangeSprite>();

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>();

avatar image Ermiq polarfuchs23 · Mar 04, 2021 at 10:56 AM 1
Share

Yeah, like that. The thing is when you did that with new you created a new ChangeSprite instance that is not connected to any game object, and it had no gameObject reference. And you should get the concrete instance of type ChangeSprite that is actually attached to the game object, and you should do this with GetComponent.

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.

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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Show gameobject as part of UI 1 Answer

Sprite Rendering Order in layer based on 'parent' Gameobject y.position in world. 0 Answers

Unity streching sprite gameobject to fit two positions. 1 Answer

how to control spriterRenderer in childrens 1 Answer


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