Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TheRealKuha · May 08, 2016 at 03:24 PM · scripting problemrigidbodyaddforceinstantiate prefabseconds

Spawning a Ball going forward every 5 seconds error, help!

Alright so I made this script:

 using UnityEngine;
 using System.Collections;
 
 public class CannonShots : MonoBehaviour {
     public Transform ballPos;
     public Rigidbody Ball;
    public  float timeMin = 1;
    public float timeMax = 3;
    public float forceAmount = 1500;
 
     void SpawnBall()
     {
 
         Rigidbody ballRigid;
         ballRigid = Instantiate(Ball, ballPos.position, ballPos.rotation) as Rigidbody;
         ballRigid.AddForce(ballPos.forward * forceAmount);
     }
     void Start() {
         float timeToSpawn = Random.Range(timeMin, timeMax);
         InvokeRepeating("SpawnBall", timeToSpawn, timeToSpawn);
     }
 }

An error pops out saying NullReferenceException: Object reference not set to an instance of an object CannonShots.SpawnBall () (at Assets/MyAssets/Scripts/CannonShots.cs:16) . I know what this error means but I can't seem to figure out how to fix it. So can somebody please help me and explain what the error was and correct it? Thanks!

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
1
Best Answer

Answer by TBruce · May 08, 2016 at 07:04 PM

@TheRealKuha

The following fixes your error(s) and adds some debugging

 void SpawnBall()
 {
 
     Rigidbody ballRigid;
     if ((Ball != null) && (ballPos != null))
     {
         ballRigid = Instantiate(Ball, ballPos.position, ballPos.rotation) as Rigidbody;
         if (ballRigid != null)
         {
             ballRigid.AddForce(ballPos.forward * forceAmount);
         }
         else
         {
             debug.LogWarning("The instantiated item does not contain a Rigidbody");
         }
     }
     else
     {
         if ((Ball == null)
         {
             debug.LogWarning("Ball has not been assigned. Please assign the Ball");
         }
         if (ballPos == null)
         {
             debug.LogWarning("ballPos has not been assigned. Please assign the ballPos");
         }
     }
 }

Also if you want to spawn every 5 seconds you need to do it like this

 InvokeRepeating("SpawnBall", timeToSpawn, 5);
Comment
Add comment · Show 2 · 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 TheRealKuha · May 09, 2016 at 06:38 PM 0
Share

Thanks, this did it! I'm really new to Unity scripting and C# overall, do you $$anonymous$$d telling me the faults in my script?

avatar image TBruce TheRealKuha · May 09, 2016 at 06:54 PM 0
Share

@TheReal$$anonymous$$uha

  1. As previously mentioned to repeat the spawn every 5 seconds you needed a value of 5 as the last parameter not timeToSpawn

  2. You were trying to instantiate an object without validating that you had a valid prefab and transform

  3. You were trying to use ballRigid after instantiating an object without validating it

avatar image
1

Answer by Dext · May 08, 2016 at 06:41 PM

Have you assigned an object to the "ballPos and Ball" variables in the inspector ? I think the variables have a value of NULL. That is why it is called a NullReferenceException error.

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 TheRealKuha · May 09, 2016 at 06:17 PM 0
Share

Yes I have, but it still gives me the error. I can play the scene but after the object spawns it pauses the game and prints the error. I can play it if I turn off the error pause but I worry it will become an issue later on. alt text

e7b13f69602845445127dd578acf3459.jpg (74.9 kB)

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

65 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

Related Questions

rigidbody,addforce is affecting all clones which are clicked on through raycast 0 Answers

Using rb.velocity causes low gravity. 2 Answers

How to move a kinematic rigidbody with touch? 0 Answers

Terminal velocity on x and y axis is higher than normal when force is applied to both axis simultaneously. 0 Answers

Rigidbody AddForce() stops working after walking away for a while 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