• 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 sacredgeometry · Apr 04, 2011 at 03:24 PM · nullreferenceexception

A little help working out some Odd Errors (UnityEngine Errors)

alt text

Hi everyone i'm having some odd errors that started appearing when writing this script, i'm normally ok at debugging but im at an end with this one and wonder if some extra eyes could help me understand what the problem is.

using UnityEngine; using System.Collections;

public class RayTest : MonoBehaviour { #region Main Fields

public float rayLength = 25.0f;

//PlayerMove public float playerMoveSpeed = 150;

//public Transform placeHolder;

//Creates a ray object named aimRay private Ray aimRay; private Ray aimRayBounce;

//Creates RaycastHits to store information from objects colliding with the ray static RaycastHit rayHit01;

// For the gameobject and hit transform private Transform playerTrans; Transform reflectedTrans;

//Raybounce Vector private Vector3 rayBounceVector = rayHit01.transform.position;

endregion

region aimLine Fields

static LineRenderer aimLine;

endregion

void Awake () { //Get this transform (might not need) playerTrans = this.GetComponent<Transform>();

}

void Start () { //aimLine (the drawn line) Initialisation aimLine = gameObject.AddComponent<LineRenderer>(); aimLine.SetVertexCount(3); aimLine.SetWidth(0.14f, 0.14f); aimLine.SetColors(Color.red, Color.red); aimLine.useWorldSpace = true;

}

void Update () { #region PlayerMovement //Player Rotation Method Call (TEMP) PlayerRotate(); #endregion

 //cast Debug.DrawRay for checking ray length
 Debug.DrawRay(transform.position, transform.up * rayLength);

 //--------------------------------------------------------------------------

 //Raycast Code      
 aimRay = new Ray(playerTrans.position,transform.forward);
 if(Physics.Raycast(aimRay.origin,transform.up, out rayHit01, rayLength))
 {


     Debug.Log("You hit a collider");
     Debug.Log("Name: " + rayHit01.collider.name + 
               " - Position: "+ rayBounceVector + 
               " - Distance: " + rayHit01.distance +
               " - Normals: " + rayHit01.normal);

     aimLine.enabled = true;

     Debug.DrawRay(rayHit01.point, Vector3.Reflect(rayHit01.point, rayHit01.normal));


 }
 else
 {
     //The ray is not colliding with anything
     Debug.Log("Not colliding");

     //aimLine01
     aimLine.enabled = false;
 }   
 //aimLine01
 aimLine.SetPosition(0, transform.position);
 aimLine.SetPosition(1, rayHit01.point);

 //TEMP: sets the final position to be the placeholder when finnished there should be an if
 //statement which askes if a colliders been hit, set to rayhitpoint else set to the end of the
 //rays length with ... ray.getpoint
 //aimLine.SetPosition(2, placeHolder.position);

}

// Rotate: Player move script (TEMP) void PlayerRotate () { float amtToMove = Input.GetAxis("Horizontal") playerMoveSpeed Time.deltaTime; transform.Rotate(0,0,amtToMove,Space.Self); }}

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

1 Reply

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

Answer by DaveA · Apr 04, 2011 at 03:42 PM

It's probably this line:

private Vector3 rayBounceVector = rayHit01.transform.position;

This code would be called during the constructor, but rayHit01 (or its transform) is not yet set up. For this reason I believe one should do such assignments in the Awake or Start function.

Comment
Add comment · 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 sacredgeometry · Apr 04, 2011 at 03:44 PM 0
Share

ill give a try, thank you for the quick reply.

avatar image sacredgeometry · Apr 04, 2011 at 03:46 PM 0
Share

If I do the assignment inside the start or awake its out of scope :S

avatar image sacredgeometry · Apr 04, 2011 at 03:47 PM 0
Share

p.s. sorry for being so inept at this, its been a while since i've done any scripting.

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

No one has followed this question yet.

Related Questions

NullReferenceException help 1 Answer

NullReferenceException while trying to access script on object 2 Answers

instantiate prefab object to existing object 1 Answer

NullReferenceException in a GUI Dialog Method 1 Answer

Gameobject.find & my 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