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 vurtual · Jan 13, 2021 at 03:40 PM · webglwindowsdebug

NullReferenceException in build not in editor,NullReferenceException in builds not in editor

Hey, thanks for taking the time to read this.

Getting a NullReferenceException on my Windows Standalone x86_64 build. Using Development Build and Script Debugging I have found the following error:

 NullReferenceException
   at (wrapper managed-to-native) UnityEngine.GameObject.get_transform(UnityEngine.GameObject)
   at Paddles.AutomatePaddles (System.Single deltaSpeed) [0x00001] in E:\Unity\Quad Pong Xtreme\Assets\Script\Paddles.cs:79 
   at Paddles.Update () [0x00033] in E:\Unity\Quad Pong Xtreme\Assets\Script\Paddles.cs:32 
 
 (Filename: E:/Unity/Quad Pong Xtreme/Assets/Script/Paddles.cs Line: 79)

What confuses me is that this refers to a very straightforward line of code:

 Vector3 ballPositionV3 = ball.transform.position;

In case it's useful, here's the whole script. The variable ball is set in the inspector;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Paddles : MonoBehaviour
 {
     [SerializeField] List <GameObject> balls = new List<GameObject>();
     [SerializeField] bool isAutomated;
     [SerializeField] float paddleSpeed = 15f;
     [SerializeField] float paddleLimit = 3.2f;
     [SerializeField] float aiSenseRange = 3.7f;
     [SerializeField] GameObject ball;
 
     float moveX;
     float moveY;
     ParticleSystem ps;
     GameplayManager gm;
     float aiDetectionPoint;
 
 
     private void Start()
     {
         gm = FindObjectOfType<GameplayManager>();
         aiDetectionPoint = paddleLimit - aiSenseRange;
         ps = transform.GetChild(0).GetChild(0).gameObject.GetComponent<ParticleSystem>();
     }
 
     private void LateUpdate()
     {
         float deltaSpeed = paddleSpeed * Time.deltaTime;
         if(gm.GetCurrentSceneName() == "Preload" || isAutomated) {
             transform.Translate(AutomatePaddles(deltaSpeed));
         } else {
             PlayerInput(deltaSpeed);
             PaddleMove();
         }
         ApplyPaddleLimit();
     }
 
     void PlayerInput(float deltaSpeed) {
         moveX = Input.GetAxis("Horizontal") * deltaSpeed;
         moveY = Input.GetAxis("Vertical") * deltaSpeed;
     }
 
     void PaddleMove() {
         switch(tag) {
             case "Bounce Horizontal":
                 transform.Translate(moveX, 0, 0);
             break;
             case "Bounce Vertical":
                 transform.Translate(0, moveY, 0);
             break;
 
         }
     }
 
     void ApplyPaddleLimit() {
         Vector3 position = transform.position;
         switch(tag) {
             case "Bounce Vertical":
                 if(transform.position.y > paddleLimit) {
                     position.y = paddleLimit;
                 } else if(transform.position.y < -paddleLimit) {
                     position.y = -paddleLimit;
                 }
             break;
             case "Bounce Horizontal":
                 if(transform.position.x > paddleLimit) {
                     position.x = paddleLimit;
                 } else if(transform.position.x < -paddleLimit) {
                     position.x = -paddleLimit;
                 }
             break;
         }
         transform.position = position;
     }
 
     Vector2 AutomatePaddles(float deltaSpeed) {
         Vector3 ballPositionV3 = ball.transform.position; // <-- Line 79
         Vector2 ballPosition = new Vector2(ballPositionV3.x, ballPositionV3.y);
         Vector2 ballVelocity = ball.GetComponent<Rigidbody2D>().velocity;
         Vector2 translate = new Vector2(0, 0);
 
         switch(tag) {
             case "Bounce Vertical":
                 if((ballVelocity.x < 0 && ballPosition.x < -aiDetectionPoint) || (ballVelocity.x > 0 && ballPosition.x > aiDetectionPoint)) {
                     translate.y = ballPosition.y - transform.position.y;
                     if(Mathf.Abs(translate.y) > deltaSpeed) {
                         if(translate.y < 0) {
                             translate.y = -deltaSpeed;
                         } else {
                             translate.y = deltaSpeed;
                         }
                     }
                 }
             break;
             case "Bounce Horizontal":
                 if((ballVelocity.y > 0 && ballPosition.y > aiDetectionPoint) || (ballVelocity.y < 0 && ballPosition.y < -aiDetectionPoint)) {
                     translate.x = ballPosition.x - transform.position.x;
                     if(Mathf.Abs(translate.x) > deltaSpeed) {
                         if(translate.x < 0) {
                             translate.x = -deltaSpeed;
                         } else {
                             translate.x = deltaSpeed;
                         }
                     }
                 }
             break;
         }
         return translate;
     }
 
     public void PrepareToDestroyPaddle() {
         GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);
         ps.Play();
         Invoke("Kill", 2);
     }
 
     void Kill() {
         Destroy(gameObject);
     }
 }
 




,Hey, thanks for taking the time to read this.

I actually want this as a WebGL build, but even in dev mode, the WebGL wasn't giving me a usable set of errors, so I did a Windows build with dev and script debugging on. The below is the error that is caught, followed by the very simple code it references

 NullReferenceException
   at (wrapper managed-to-native) UnityEngine.GameObject.get_transform(UnityEngine.GameObject)
   at Paddles.AutomatePaddles (System.Single deltaSpeed) [0x00001] in E:\Unity\Quad Pong Xtreme\Assets\Script\Paddles.cs:79 
   at Paddles.Update () [0x00033] in E:\Unity\Quad Pong Xtreme\Assets\Script\Paddles.cs:32 
 
 (Filename: E:/Unity/Quad Pong Xtreme/Assets/Script/Paddles.cs Line: 79)


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Paddles : MonoBehaviour
 {
     [SerializeField] List <GameObject> balls = new List<GameObject>();
     [SerializeField] bool isAutomated;
     [SerializeField] float paddleSpeed = 15f;
     [SerializeField] float paddleLimit = 3.2f;
     [SerializeField] float aiSenseRange = 3.7f;
     [SerializeField] GameObject ball;
 
     float moveX;
     float moveY;
     ParticleSystem ps;
     GameplayManager gm;
     float aiDetectionPoint;
 
 
     private void Start()
     {
         gm = FindObjectOfType<GameplayManager>();
         aiDetectionPoint = paddleLimit - aiSenseRange;
         ps = transform.GetChild(0).GetChild(0).gameObject.GetComponent<ParticleSystem>();
     }
 
     private void LateUpdate()
     {
         float deltaSpeed = paddleSpeed * Time.deltaTime;
         if(gm.GetCurrentSceneName() == "Preload" || isAutomated) {
             transform.Translate(AutomatePaddles(deltaSpeed));
         } else {
             PlayerInput(deltaSpeed);
             PaddleMove();
         }
         ApplyPaddleLimit();
     }
 
     void PlayerInput(float deltaSpeed) {
         moveX = Input.GetAxis("Horizontal") * deltaSpeed;
         moveY = Input.GetAxis("Vertical") * deltaSpeed;
     }
 
     void PaddleMove() {
         switch(tag) {
             case "Bounce Horizontal":
                 transform.Translate(moveX, 0, 0);
             break;
             case "Bounce Vertical":
                 transform.Translate(0, moveY, 0);
             break;
 
         }
     }
 
     void ApplyPaddleLimit() {
         Vector3 position = transform.position;
         switch(tag) {
             case "Bounce Vertical":
                 if(transform.position.y > paddleLimit) {
                     position.y = paddleLimit;
                 } else if(transform.position.y < -paddleLimit) {
                     position.y = -paddleLimit;
                 }
             break;
             case "Bounce Horizontal":
                 if(transform.position.x > paddleLimit) {
                     position.x = paddleLimit;
                 } else if(transform.position.x < -paddleLimit) {
                     position.x = -paddleLimit;
                 }
             break;
         }
         transform.position = position;
     }
 
     Vector2 AutomatePaddles(float deltaSpeed) {
         Vector2 ballPosition = ball.transform.position; <---- Line 79
         Vector2 ballVelocity = ball.GetComponent<Rigidbody2D>().velocity;
         Vector2 translate = new Vector2(0, 0);
 
         switch(tag) {
             case "Bounce Vertical":
                 if((ballVelocity.x < 0 && ballPosition.x < -aiDetectionPoint) || (ballVelocity.x > 0 && ballPosition.x > aiDetectionPoint)) {
                     translate.y = ballPosition.y - transform.position.y;
                     if(Mathf.Abs(translate.y) > deltaSpeed) {
                         if(translate.y < 0) {
                             translate.y = -deltaSpeed;
                         } else {
                             translate.y = deltaSpeed;
                         }
                     }
                 }
             break;
             case "Bounce Horizontal":
                 if((ballVelocity.y > 0 && ballPosition.y > aiDetectionPoint) || (ballVelocity.y < 0 && ballPosition.y < -aiDetectionPoint)) {
                     translate.x = ballPosition.x - transform.position.x;
                     if(Mathf.Abs(translate.x) > deltaSpeed) {
                         if(translate.x < 0) {
                             translate.x = -deltaSpeed;
                         } else {
                             translate.x = deltaSpeed;
                         }
                     }
                 }
             break;
         }
         return translate;
     }
 
     public void PrepareToDestroyPaddle() {
         GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);
         ps.Play();
         Invoke("Kill", 2);
     }
 
     void Kill() {
         Destroy(gameObject);
     }
 }
 


Comment
Add comment · Show 4
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 zereda-games · Jan 13, 2021 at 03:56 PM 0
Share

ball cannot be found? ok easiest way i found to fix this across both build and non build is do a simple check in Awake or start:

 void Awake()
 {
     if(ball == null) 
     { 
        ball = GameObject.FindObjectOfType<Ball>(); // if it has a script on it, otherwise 
        //ball = GameObject.FindGameObjectWithTag("Ball");// then add this tag to the ball.
     }
  }
avatar image zereda-games zereda-games · Jan 13, 2021 at 03:57 PM 0
Share

oh looks like after looking at code a bit your getting the ball from a list of possible balls, so when you Instantiate the ball from the list set:

  ball = balls[selection]; 
avatar image zereda-games zereda-games · Jan 13, 2021 at 03:59 PM 0
Share

If thats the only error you are getting try that make a new build and see if it solves the issue or if a new one arises.

avatar image zereda-games zereda-games · Jan 13, 2021 at 04:05 PM 0
Share

I personally don't like setting things in the inspector that much and i make my code do as much of the work as possible so when i hit play the code populates with the available scene objects or in not found will load a prefab and instantiate when called like a window panel for example. It's just one way of doing things. Yes, it's a lot more code but i enjoy writing code so that's not a problem to $$anonymous$$$$anonymous$$ It's not for everyone but if you always make your code do the work, you have less of a chance of a null reff error upon build :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by vurtual · Jan 13, 2021 at 07:03 PM

Thanks for the advice. I have done various builds - WebGL, WebGL with dev mode, Windows Standalone, Windows Standalone with dev mode, Windows Standalone with dev mode and script debugging, all so far without success.

I hope I've understood your suggestion properly, I now have:

 // variable declarations
         [SerializeField] GameObject ballPrefab;
 
 // in the Start() method
         if(ballPrefab == null) {
             ballPrefab = Resources.Load("Prefabs/Ball") as GameObject;
         }
 
 // in the Update method
         if(gm.balls.Count <=0) { //gm being the gameplay manager
             Instantiate(ballPrefab, Vector3.zero, Quaternion.identity);
         }

It still works in the editor, and fails in the Windows build, with this error:

 ArgumentException: The Object you want to instantiate is null.
   at UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) [0x00009] in <cbc2a4dc69734303bc7c8542260e1c75>:0 
   at UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) [0x00001] in <cbc2a4dc69734303bc7c8542260e1c75>:0 
   at UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) [0x00001] in <cbc2a4dc69734303bc7c8542260e1c75>:0 
   at PreloadManager.Update () [0x00033] in E:\Unity\Quad Pong Xtreme\Assets\Script\PreloadManager.cs:49 
 

so what do I attempt next?

Comment
Add comment · Show 1 · 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 vurtual · Jan 14, 2021 at 10:42 AM 0
Share

I've solved this. Simply put, the syntax for Resources.Load has changed from what I had found, and the prefab has to be in a folder named Resources in the Assets folder. It can be in a further subfolder though. I put my ball prefab in Assets/Resources/Prefabs and used the following code:

 ball = Resources.Load<GameObject>("Prefabs/Ball");

I do have to work out how to do the same for Audio now, but that's a new question/

Thank you to @zareda-games for getting me on the right track

avatar image
0

Answer by NathoSteveo · Jan 14, 2021 at 10:50 AM

it's good to keep mind if using models through script to enable read/write


alt text


readwrite.jpg (45.0 kB)
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

124 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

Related Questions

Touch in WebGL on Windows, STILL broken? 2 Answers

Debugging WebGL/Windows Standalone 0 Answers

Why is Debug.Log so much slower on Windows than Mac? 3 Answers

WebGL build size is much smaller compared to PC build size 0 Answers

Using unity as a library for creating windows, mac and web application 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