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 bruflot · May 28, 2012 at 12:25 PM · velocitynullreferenceexceptioncharacter controller

Vector3 TransformDirection "object reference"-error

Hello!

 Transform thisTransform;
 Public Vector3 direction = Vector3.zero;
 
 void Awake(){
     thisTransform = GetComponent<Transform>();
     direction = thisTransform.TransformDirection(Vector3.forward); //This line!
 }

This is where I get the "Object reference not set to an instance of an object" error. The thing is, I have defined thisTransform as an object, so what more is there to "define"?

Any ideas?

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

Answer by whydoidoit · May 28, 2012 at 12:31 PM

You haven't put the character controller in the controller variable I'm guessing. The normal way to do this would be to use GetComponent:

 void Start() {
     controller = GetComponent(CharacterController);
 }
Comment
Add comment · Show 7 · 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 bruflot · May 28, 2012 at 12:38 PM 0
Share

Actually, I did, but forgot to include it in my sample -.- Hang on a $$anonymous$$ute, I'll edit my entire post.

avatar image whydoidoit · May 28, 2012 at 12:46 PM 0
Share

Well you can just get the transform from "transform" no need for the GetComponent!

avatar image whydoidoit · May 28, 2012 at 12:48 PM 0
Share

Actually it looks like you just want to do

 direction = transform.forward;
avatar image Bunny83 · May 28, 2012 at 01:29 PM 0
Share

No he want's to cache the transform so it'S ok. And yes, it doesn't matter if you use .transform or Getcomponent() it's the same. However the problem is that you shouldn't access other components in Awake. Awake is called immediately when the object is created. That means other components might not be there at this point.

For component cross references use Start() which is executed after all objects are created and initialized.

And finally this:
thisTransform.TransformDirection(Vector3.forward);
equals this:
thisTransform.forward;

avatar image bruflot · May 28, 2012 at 01:29 PM 0
Share

Whydoidoit, it still returns the same error.

Show more comments
avatar image
0

Answer by Owen-Reynolds · May 28, 2012 at 06:19 PM

It's fine in Awake. I tried it exactly as written (well, the P in public should be lower-case.) The docs even say Awake is called after all objects are created (which I didn't realize -- thought it was called after each individually was created.)

But, yeah, the error is probably saying that thisTransform is null. Try printing it to be sure. Is the script on some odd object that doesn't have a transform? (not sure how -- even empties have transforms.)

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 whydoidoit · May 28, 2012 at 06:23 PM 0
Share

First, just a note: when @Bruflot posted this it was a different problem, which makes this whole thing look very confusing now :) For instance, there was not a Start or an Awake!

But to your point, I'm pretty sure that all Components are ready in Awake, but not any potential children due to an Instantiate of a connected set of objects. Is that your experience?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

NullReferenceException: Object reference not set to an instance of an object CannonFire.Update 0 Answers

Character Controller Respawn (Rotation Issue) 1 Answer

how can I make a character fly when holding down the jump button? 1 Answer

Restricting character controllers velocity after hitting a wall 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