Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bryanbas · Oct 04, 2015 at 04:38 PM · javascriptvariableother script

nullrefrenceexception: how can i solve this?

so, ive been looking for 3 hours now, and i cant find why this is giving me an error, what im trying to do, is to lock the player (maxsidewaysspeed = 0.0f, jumping = false) and set the gravity, to get a thwomp powerup effect, so what im doing is: detecting if the player has obtained the speed powerup (you see it trying to check if the player has a maxSidewaysSpeed of 9.0f, wich is the value that the boost gives) then im checking if the player attacks, then i set all the variables. after that in the else, im setting the values back with the speedBoost value from earlier. im trying to access the standard script CaracterMotor. they arent on the same gameObject, nor are they in the same folder. here is my code:

 #pragma strict
 
 static var attack : boolean;
 static var speedBoost : boolean;
 
 function Start () {
 
 }
 function Update () {
     
         if(script.movement.maxSidewaysSpeed == 9.0f){
             speedBoost = true;
         }
         else{
             speedBoost = false;
         }
     
     
     
 var script : CharacterMotor = GetComponent(CharacterMotor);
     
     var AT = gameObject.GetComponent(AnimateTexture);
     if(Input.GetMouseButton(0) && attack){
         AT.rowNumber = 4; 
         script.movement.maxSidewaysSpeed = 0.0f;
         script.movement.gravity = 70.0f;
         script.jumping.jumping = false;
     }
       else if(Input.GetKey("a")){
         AT.rowNumber = 3;
     } else if(Input.GetKey("d")){ 
         AT.rowNumber = 1; 
     } else if(Input.GetKey("left")){
         AT.rowNumber = 3;
     } else if(Input.GetKey("right")){
         AT.rowNumber = 1;
     } else { 
     AT.rowNumber = 0; 
     if(speedBoost){
         script.movement.maxSidewaysSpeed = 9.0f;
         script.movement.gravity = 20.0f;
         script.jumping.jumping = true;
     }
     else{
         script.movement.maxSidewaysSpeed = 6.0f;
         script.movement.gravity = 20.0f;
         script.jumping.jumping = true;
     }
     }
 }

please help me, im stuck!

thanks in advance!

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

Answer by Oribow · Oct 04, 2015 at 05:42 PM

If your CharacterMotor isnt on the same GameObject, why are you trying to find it on the same GameObject? Look at this line:

 var script : CharacterMotor = GetComponent(CharacterMotor);

"GetComponent" searches only on the GameObject the script is attached to! Try to make it a public var and set it in the Inspector.

 public var script : CharacterMotor;



Comment
Add comment · Show 10 · 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 meat5000 ♦ · Oct 04, 2015 at 05:44 PM 0
Share

Or use GameObject.Find

avatar image bryanbas · Oct 04, 2015 at 07:17 PM 0
Share

I know I'm really dumb at unity coding... :/

avatar image Oribow bryanbas · Oct 04, 2015 at 07:21 PM 0
Share

Could you accept the answer?

avatar image bryanbas · Oct 05, 2015 at 06:59 PM 0
Share

I can't figure it out, can you give me the debugged code please?

avatar image Oribow bryanbas · Oct 05, 2015 at 07:21 PM 0
Share

Whats the problem? Just exchange your line quoted in my post, with the second line quoted in my post and set the var "script" in the inspector.

avatar image bryanbas · Oct 06, 2015 at 02:23 PM 0
Share

@Oribow Well i cant set a script in that box

avatar image bryanbas · Oct 07, 2015 at 07:29 PM 0
Share

Hello? It's not working?

avatar image Oribow bryanbas · Oct 07, 2015 at 07:39 PM 0
Share

Ok try this:

 #pragma strict
  
 static var attack : boolean;
 static var speedBoost : boolean;
 public var script : Character$$anonymous$$otor; 
 
 function Start () {
  
 }
 function Update () {
      
     if(script.movement.maxSidewaysSpeed == 9.0f){
         speedBoost = true;
     }
 else{
             speedBoost = false;
 }
      
      
      
 
      
 var AT = gameObject.GetComponent(AnimateTexture);
 if(Input.Get$$anonymous$$ouseButton(0) && attack){
     AT.rowNumber = 4; 
     script.movement.maxSidewaysSpeed = 0.0f;
     script.movement.gravity = 70.0f;
     script.jumping.jumping = false;
 }
 else if(Input.Get$$anonymous$$ey("a")){
     AT.rowNumber = 3;
 } else if(Input.Get$$anonymous$$ey("d")){ 
     AT.rowNumber = 1; 
 } else if(Input.Get$$anonymous$$ey("left")){
     AT.rowNumber = 3;
 } else if(Input.Get$$anonymous$$ey("right")){
     AT.rowNumber = 1;
 } else { 
     AT.rowNumber = 0; 
     if(speedBoost){
         script.movement.maxSidewaysSpeed = 9.0f;
         script.movement.gravity = 20.0f;
         script.jumping.jumping = true;
     }
     else{
         script.movement.maxSidewaysSpeed = 6.0f;
         script.movement.gravity = 20.0f;
         script.jumping.jumping = true;
     }
 }
 }


Show more comments

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

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

how do i make an object destroy after multiple collisions?,How do i create a script that destroys an object after multiple collisions? 0 Answers

Help with variables 1 Answer

Show in text certain variables 0 Answers

Error BCE0051 with script JS 1 Answer

How to Modify a java Script variable with a c# Script 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