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 leadpixel · Apr 27, 2011 at 11:04 PM · prefabnullreferenceexception

very weird NullReferenceException error causeing one script to break another

Having an incredibly annoying isue with a new script and if anyone could shed somelight on th issue it would be much appreciated.

I have a script i'm using to allow gameobjects to orbit a larger gameobject which is acting like a planet:

var rotationSpeed = 120.0; var translationSpeed = 10.0; var height = 2.0; //height from ground level private var centre : Transform; //transform for planet private var radius : float; //calculated radius from collider var planet : SphereCollider; //collider for planet

function Start ()

 {
     //consider scale applied to planet transform (assuming uniform, just pick one)
     radius = planet.radius * planet.transform.localScale.y;
     centre = planet.transform;
     //starting position at north pole
     transform.position = centre.position + Vector3(0,radius+height,0);
 }

 function Update ()

 {
     //translate based on input
     var inputMag  = Input.GetAxis("Fire1")*translationSpeed*Time.deltaTime;
     transform.position += transform.forward * inputMag;
     //snap position to radius + height (could also use raycasts)
     targetPosition = transform.position - centre.position;
     var ratio = (radius + height) / targetPosition.magnitude;
     targetPosition.Scale(Vector3(ratio, ratio, ratio) );
     transform.position = targetPosition + centre.position;
     //calculate planet surface normal
     surfaceNormal = transform.position - centre.position;
     surfaceNormal.Normalize();
     //GameObject's heading
     var headingDeltaAngle = Input.GetAxis("Fire2") * Time.deltaTime * rotationSpeed;
     headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up);
     //align with surface normal
     transform.rotation = Quaternion.FromToRotation( transform.up, surfaceNormal) * transform.rotation;
     //apply heading rotation
     transform.rotation = headingDelta * transform.rotation;
 }

This code is going to be accessed by two prefabs which will be spawned onto the planet. The first one is called tornadogameobject and i have(had) a working script which aquired the the planet gameobject (Sphere). I tested the script and it was working perfectly:

    var sphereColl : SphereCollider;
function Start ()
{
sphereColl = GameObject.Find("Sphere").GetComponent(SphereCollider) as SphereCollider;
transform.Find("tornadogameobject").GetComponent("newgrav").planet = sphereColl;
}

I then made another version of the script attaching it to the other prefab, only changing the name found to reboundcirclegameobject:

    var sphereColl : SphereCollider;
function Start ()
{
    sphereColl = GameObject.Find("Sphere").GetComponent(SphereCollider) as SphereCollider;
    transform.Find("reboundcirclegameobject").GetComponent("newgrav").planet = sphereColl;
}

when i tested this prefab, it came up with a NullReferenceException, which is surely strange as the other version of the script worked perfectly. On top of that when i went to retest the other prefab it had now stopped working also bringing up the NullReferenceException, very strange considering it had previously worked perfectly. I tested the tornadogameobject prefab on its own and with the reboundcirclegameobject and it made no difference.

I'm at a bit of a loss at what to do right now as the really doesn't make any sense to me as to why this would happen. Are the two scripts cancelling each other out because they both want to access the same GameObject? if anyone could shed some light on this issue i would be eternally gratefull.

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

Answer by Alec-Slayden · Apr 28, 2011 at 03:14 AM

This seems to be the problem:

targetPosition = transform.position - centre.position;

That's line 26, sending out the null reference exception. This is a get_position exception, and it's the first time the Update uses the centre transform.

What this means:

It would appear your newgrav Start() is running before your prefab Start(). This means that This:

centre = planet.transform;

is assigning a null value to centre, because there isn't a collider assigned to planet yet.

change the prefab scripts to Awake(), which is run first, to ensure the collider gets assigned before centre does.

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

Answer by DaveA · Apr 27, 2011 at 11:10 PM

Make sure you have a game object with that exact spelling (Case Is Important) in your scene. Use Debug.Log statements to verify things are getting where they should. And/or use the debugger. I usually break up the line something like:

var go = transform.Find("reboundcirclegameobject");
if (go)
  var ng = go.GetComponent("newgrav");
if (ng)
  ng.planet = sphereColl;

so it's easier to see which line is failing (what's not found) and you can put Debug.LogError statements in 'else' there too.

Comment
Add comment · Show 4 · 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 leadpixel · Apr 27, 2011 at 11:20 PM 0
Share

I will give that a go asap thanks very much. I am leaning towards the issue being something wrong with unity itself though. I was just giving another playtest and its applying the script but still telling me there's a null reference, when there obviously isn't as the scripts doing what its supposed to. I changed the height variable on the newgrav script to check and its affecting the prefab. The other script that previously had worked is still failing though, have you ever heard of something like this before??

avatar image Eric5h5 · Apr 27, 2011 at 11:34 PM 0
Share

@loadpixel: it's not Unity. If you have a null reference exception, you have failed to get a reference (it's therefore null), simple as that.

avatar image leadpixel · Apr 27, 2011 at 11:49 PM 0
Share

It must be getting a reference or it would be flying of the Sphere, as it was doing prior to its sudden change of heart. Also why would the other prefab (tornadogameobject) stop referencing, when i had previously playtested it and it worked perfectley, and have since made no changes to the script? that just doesn't make any sense to me.

avatar image leadpixel · Apr 28, 2011 at 12:56 AM 0
Share

it has reverted back to flying of the sphere again, but i have checked in the inspector and is assigning the reference Sphere. I took a screencapture as i thought this might be a little clearer. As you can see the reference is being assigned, but for some reason ignoreing it: http://youtu.be/FpzczGYUrVI

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

No one has followed this question yet.

Related Questions

NullReferenceException from a script after linking prefab with drag and drop in inspector. WTF? 1 Answer

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

Show and Hide a prefab or GameObject 10 Answers

cloned game object wont call script right... 2 Answers

NullReferenceException problem 2 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