• 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 /
  • Help Room /
avatar image
Question by jeisinn · Jun 24, 2022 at 12:14 AM · instantiatevrnull reference exception

Object does not instantiate, causing an error while attempting to get a component.

I am presently working on translating a VR dodgeball game to the Oculus Quest 2, which sees the player use a ball to deflect oncoming balls. In this game, as the "Start Game" button is pressed, a coroutine should start, spawning the dodgeballs in the throwers' hands. In the Vive/SteamVR version of the application, the menu cannot be accessed by the player in the headset, instead an administrator manages the menu from the computer. The coroutine is shown below:

 public void spawnBall() {
             ball = Instantiate(ballPrefab, ballSpawnLocation.position, Quaternion.identity);
             ball.transform.localScale = new Vector3(EnemyController.ballSize, EnemyController.ballSize, EnemyController.ballSize);
             ball.transform.parent = ballSpawnLocation;
             ball.transform.rotation = Quaternion.LookRotation(-Vector3.forward);
 
             if (listener) {
                 GameObject.Find("StartGameButton").GetComponent<Button>().onClick.RemoveAllListeners();
                 listener = false;
             }
         }

I have recreated the menu so that the player inside the headset can access it and start the game. However, when the start button is pressed by the player, the coroutine does not run, and no balls spawn inside the throwers' hands. This results in a null reference exception, as the script controlling the throwers attempts to reference the Rigidbody component attached to the ball prefab.

 Rigidbody ballRigid = ball.GetComponent<Rigidbody>();

The ball never spawns, which also causes the throwers' animation to activate, however it does not reset into the idle animation. I am currently attempting to have the coroutine run again as the Start Game button is pressed, however I am unsure how to approach it.

Relevant photos are attached to this link: https://drive.google.com/drive/folders/13hM3iZpskkl-dk72PT0IPac29lnswRCB?usp=sharing

Comment

People who like this

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

Answer by wideeyenow_unity · Jun 24, 2022 at 12:58 PM

I had an issue quite similar, the problem was my declaration of the object to be referenced. My exact issue was:

         body = transform.Find("Body").gameObject;
 
         shoulderL = transform.Find("Shoulder L").gameObject;
 
         shoulderR = transform.Find("Shoulder R").gameObject;
 
         armL = transform.Find("Shoulder L/Arm L").gameObject;
 
         armR = transform.Find("Shoulder R/Arm R").gameObject;

I didn't know that a "/" needed to be placed for children within children, as armL was just Find("Arm L") before. My null reference was only on Arm L(and a GetComponent of it), and nothing worked after it. No other errors, even though the other arm would have had the same issue. What is your error exactly, and where?

Comment
jeisinn

People who like this

1 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 jeisinn · Jun 24, 2022 at 01:20 PM 0
Share

My issue is specifically with the Rigidbody line, as it attempts to reference the Rigidbody on an object that was never instantiated. This also affects the throwers, since they no longer have a ball to throw, and it causes their throwing animation to run without returning to their idle animation on completion.

avatar image wideeyenow_unity jeisinn · Jun 24, 2022 at 02:29 PM 0
Share

You do not show the implementation of the rigidbody code, so I'm not fully sure on how that starts checking things. But I know a lot of people always refer to using Object Pooling, having all the objects needed spawn up during load, but if they are not used to be set to Inactive, and reside at (0,0,0) or wherever not important. And made active, and moved to proper position when they are needed. Which Object pooling also states to not destroy the object, simply make it inactive again and move to an unused location. And if you don't want to do Object Pooling, make sure the check for the rigidbody isn't called until the ball is instantiated. Which may, depending on your code setup, also require you to set that component/object to be inactive as well. And if you're using a parent class, the parent class will need to be the one setting active, or inactive, as the child object itself once Inactive will not read the code to turn itself on

avatar image jeisinn wideeyenow_unity · Jun 27, 2022 at 12:51 PM 0
Share

This is the function that gets the ball rigidbody, not including equations for ball trajectory and velocity:

  void ballLaunch() {
             Rigidbody ballRigid = ball.GetComponent<Rigidbody>();
 
             // Throw preparation
             ball.transform.parent = null;    // Removes parent
             ballRigid.useGravity = true;    // Turns on ball gravity
             ballRigid.constraints = RigidbodyConstraints.None;      // Removes positional constraints   
 
             ballPrefab.GetComponent<OnBallCollision>().StartTimeOut();        // Begins the timeout for the ball (if it hits nothing, count as a fail)
 
             GetComponent<Animator>().SetBool("IsThrowing", false);      // Stop the throwing animation
            

The rigidbody is referenced once the ball is to be launched, but since the ball is never instantiated, it returns the null reference exception.

avatar image

Answer by jeisinn · Jul 05, 2022 at 12:30 PM

I figured out that this was the result of me having more than one UI object with the startGame function. It seems like the game becomes confused when more than one button starts the game, and does not instantiate the balls as expected.

Comment

People who like this

0 Show 0 · 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

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

Getting 'Object Reference not set' when calling function from instantiated object 1 Answer

"I am getting a null reference exception error for Pathfollowertest.instance.centerslot.Add(Cube.transform) 0 Answers

"InvalidCastException" And "NullReferenceException" Error 1 Answer

NullReferenceException in Two Scripts 1 Answer

Why cant i instantiate my ship? Error NullReferenceException: Object reference not set to an instance of an object. 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