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 CarloK · Jun 10, 2015 at 07:40 PM · prefabnullreferenceexceptionruntimescriptingbasics

NullReferenceException after adding a script at runtime

Hi there! I'm working on a unity project for university with other students together. Each of us has to build a spaceship and a level with the intention that all spaceships are able to race in all levels. In my level I want the ships to be able to shoot stuff and I wrote a very simple script to allow this. As I can't add anything to the ships of my teammates manualy, I want to add the script to the ships at runtime if my level is selected. I have a bullet prefab that I use to clone the bullets and if I add my script manually to a ship it works, I can shoot. But if I add it at runtime, I get the following error each time I try to shoot:

NullReferenceException: Object reference not set to an instance of an object T4PlayerShoot.Update () (at Assets/T4/T4PlayerShoot.cs:35)

I guess after adding the script at runtime it loses the reference to the bullet.prefab. How can I fix this? Here is my relevant code: in my levellogic:

     void OnGUI() {
     //...
         var ship_objects = GameObject.FindGameObjectsWithTag("Ship");
         foreach(var ship_object in ship_objects){
             ship_object.AddComponent<T4PlayerShoot>();
             }
     //...
     }

the shooting script:

 public class T4PlayerShoot : MonoBehaviour {
 
     public Rigidbody bullet;
     public float velocity = 10.0f;
     protected string fire;
     private bool firePressed = false;
 
     //Get the Firekey of the Controller
     protected virtual string FetchFire(Controller controller)
     {
         return controller.ctrlAxisFire;
     }
 
     private string FetchFire0()
     {
         Controller ctrl = GetComponentInParent<Controller>();
         if (ctrl != null)
             return FetchFire(ctrl);
         return null;
     }
     
     void Update () {
 
         if (fire == null) {
             fire = FetchFire0 ();
         }
 
         if (fire != null) {
             firePressed=0!=Input.GetAxis(fire);
         }
         if (firePressed) {
             Rigidbody newBullet=Instantiate(bullet,transform.position,bullet.rotation)as Rigidbody;//ERROR
             newBullet.AddForce (transform.forward*velocity,ForceMode.VelocityChange);
         }
     }
 }

Thanks for your help!

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

making prefabs at runtime? 2 Answers

Can no longer create pre-fab 1 Answer

Prefabs are being overriden during runtime and destroying components is prevented? 1 Answer

Export objects to a .3DS file at runtime 1 Answer

Destroy Prefab with UI Slider 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