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 leonardherndon · Nov 24, 2012 at 10:42 PM · errornull reference

NullReferenceException: Object reference not set to an instance of an object script_bullet.OnTriggerEnter (UnityEngine.Collider other) (at Assets/script_bullet.js:35)

I am getting an Null Reference Exception when trying to call CreateAsteroid() (which is inside my script_asteroid file) from within script_bullet. I am not exactly sure how I should be calling the asteroid. Can somebody show me what I am doing wrong? Thanks in advance.

 //Bullet Script
 #pragma strict
 
 //Inspector Variables
 
 var bulletSpeed         : float = 40.0;                //Bullet Speed
 var bulletBounds        : float = 15;                //Bullet Boundry 
 var asteroid             : script_asteroid;
 asteroid                 = GetComponent(script_asteroid);
 var explosion            : Transform;
 
 
 function Start () {
 
 }
 
 function Update () {
     var moveBullet        : float = bulletSpeed * Time.deltaTime;
     
     //Moves Player based on stored input data
     transform.Translate(moveBullet, 0,0);
     
     if(transform.position.x >= bulletBounds)
         Destroy(gameObject);
 }
 
 function OnTriggerEnter (other : Collider) 
 {
     //Print what the bullet is colliding with
     print("bullet collison: " + other.gameObject.tag);
     
     //Check for collision with asteroid
     if(other.gameObject.tag == "Asteroid")
     {
         asteroid.CreateAsteroid();
         
         //Create the explosion on impact
         Instantiate(explosion,transform.position,transform.rotation);
         Destroy(other.gameObject);
         Destroy(gameObject);
     }
     
 }
 //Player Script
 #pragma strict
 
 //Inspector Variables
 
 var asteroidSpeedHMin         : float = 1.0;                //Asteroid Horizontal Speed Min
 var asteroidSpeedHMax         : float = 6.0;                //Asteroid Horizontal Speed Max
 var asteroidSpeedV             : float = 10.0;                //Asteroid Vertical Speed
 var asteroidBoundsH            : float = 15;                //Limits for Asteroid movement Horizontal
 var asteroidBoundsV            : float = 10;                //Limits for Asteroid movement Vertical
 var asteroid                    : Transform;
 var asteroidHealth            : int    = 3;
 
 //Private Variables
 private var asteroidPosition;
 private var asteroidRotation;
 private var transH;
 private var transV;
 
 function Start () {
     asteroidPosition = Vector3(transform.position.x,Random.Range(-8,8),0);
     asteroidRotation = transform.rotation;
     transH             = -Random.Range(asteroidSpeedHMin,asteroidSpeedHMax)*Time.deltaTime;
     transV             = Random.Range(-asteroidSpeedV,asteroidSpeedV)*Time.deltaTime;
 }
 
 function Update () {
 
 
     //Moves Asteroid based on Random numbers
     transform.Translate(transH,transV,0);
     
     if(transform.position.x >= asteroidBoundsH || transform.position.x <= -asteroidBoundsH || transform.position.y >= asteroidBoundsV || transform.position.y <= -asteroidBoundsV) {
         CreateAsteroid();
         Destroy(gameObject);
     }
 }
 
 function CreateAsteroid() {
     Instantiate (asteroid, asteroidPosition, asteroidRotation);
 }
Comment
Add comment · Show 1
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 ExTheSea · Nov 24, 2012 at 10:52 PM 0
Share

Did you set the asteroid - Transform in the Editor view by dragging a Transform on it? To check this go to you gameobject on which you did attach the script and then look in the list below the script name for asteroids if after asteriods there stands None(Transform) then you need to drag a Transform (a model, prefab, ....) onto asteroids.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Nov 24, 2012 at 10:56 PM

Ok so firstly don't put code outside a function in Unity script - put the

 asteroid = GetComponent(script_asteroid);

Inside start.

Next - what is script_asteroid attached to ? Because at the moment it appears it thinks its attached to your bullet - which seems unlikely. The GetComponent is looking at the GameObject it is attached to and trying to get the asteroid component from that - which is the bullet.

It appears CreateAsteroid is actually on the player - so you need to get a reference to that. Easiest way might be a singleton on the player - like this:

 //Player Script
 #pragma strict
 
 //Inspector Variables
 
 var asteroidSpeedHMin    : float = 1.0;          //Asteroid Horizontal Speed Min
 var asteroidSpeedHMax    : float = 6.0;          //Asteroid Horizontal Speed Max
 var asteroidSpeedV         : float = 10.0;          //Asteroid Vertical Speed
 var asteroidBoundsH        : float = 15;          //Limits for Asteroid movement Horizontal
 var asteroidBoundsV        : float = 10;          //Limits for Asteroid movement Vertical
 var asteroid              : Transform;
 var asteroidHealth       : int  = 3;
 
 //Private Variables
 private var asteroidPosition;
 private var asteroidRotation;
 private var transH;
 private var transV;

 public static var instance : Player; //Presuming it's called Player!
 function Awake()
 {
       instance = this;
 }

 function Start () {
     asteroidPosition = Vector3(transform.position.x,Random.Range(-8,8),0);
     asteroidRotation = transform.rotation;
     transH          = -Random.Range(asteroidSpeedHMin,asteroidSpeedHMax)*Time.deltaTime;
     transV          = Random.Range(-asteroidSpeedV,asteroidSpeedV)*Time.deltaTime;
 }
 
 function Update () {
 
 
     //Moves Asteroid based on Random numbers
     transform.Translate(transH,transV,0);
 
     if(transform.position.x >= asteroidBoundsH || transform.position.x <= -asteroidBoundsH || transform.position.y >= asteroidBoundsV || transform.position.y <= -asteroidBoundsV) {
        CreateAsteroid();
        Destroy(gameObject);
     }
 }
 
 function CreateAsteroid() {
     Instantiate (asteroid, asteroidPosition, asteroidRotation);
 }


The any time you like you can do:

   Player.instance.CreateAsteroid();

We've put together some tutorials on this stuff on Unity Gems

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 leonardherndon · Nov 25, 2012 at 12:32 AM 0
Share

This solved it. Thank you for your help. $$anonymous$$y comments were muffed up so that is the asteroid script and not the player script.

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

12 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script not working (Java script) 2 Answers

Access components in ALL children 1 Answer

Official Unity Space Shooter Tutorial 1 Answer

"Absolute URI is too short" woe 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