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 fadilRaditya · Jul 27, 2012 at 04:40 AM · errornullreferenceexception

Error with Update and OntrrigerEnter funtion

dear all, i tried to solve this problem alone. However this problem to tough for newbie like me. logically, i want to know how much speed that player have when enter a way (just say it is cube) this is the cube code

     var data : manualscar;
     data = gameObject.GetComponent("manualscar");
     function OnTriggerEnter(other : Collider){
      if(other.tag == "Player"){
      if(data.currentSpeed < 5){
      print("5");
  }
  }
  }

and this is a player code

     //to move the manual's car.
 var maxSpeed = 0.0; 
 var minSpeed = 0.0;
 var maxAcceleration = 0.0; // Controls the acceleration
 var currentSpeed = 0.0;
 var gear = 0;
 var trackSpeed = 0;
  
 function Update() 
 { 
  //to change the max speed, min speed, and acceleration
  if(gear == 0){
  currentSpeed = 0;
  maxAcceleration = 0.0;
  minSpeed = 0;
  maxSpeed = 0;
  }
  else if(gear == 1){
  maxAcceleration = 10.0;
  minSpeed = 0;
  maxSpeed = 20;
  }
  else if(gear == 2){
  maxAcceleration = 8.0;
  minSpeed = 11;
  maxSpeed = 50;
  }
  else if(gear == 3){
  maxAcceleration = 7.5;
  minSpeed = 31;
  maxSpeed = 80;
  }
  else if(gear == 4){
  maxAcceleration = 6.0;
  minSpeed = 51;
  maxSpeed = 120;
  }
  else if(gear == 5){
  maxAcceleration = 5.0;
  minSpeed = 81;
  maxSpeed = 180;
  }
  else{
  maxAcceleration = 0.0;
  minSpeed = 0;
  maxSpeed = 0;
  }
  //to change the gear up with &quot;1&quot; and down with &quot;2&quot;
  if(Input.GetKeyDown(&quot;1&quot;)){
  if(gear != 5){
  gear++;
  }
  }
  if(Input.GetKeyDown(&quot;2&quot;)){
  if(gear != 0){
  gear--;
  }
  }
     transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
     if(Input.GetKey("w")){
      if(currentSpeed > maxSpeed){
        currentSpeed -= maxAcceleration * Time.deltaTime;
        if(gear == 4){
        trackSpeed=currentSpeed+2;
        }
        else if(gear == 3){
        trackSpeed=currentSpeed+1.5;
        }
        else if(gear == 2){
        trackSpeed=currentSpeed+1;
        }
        else if(gear == 1){
        trackSpeed=currentSpeed+0.5;
        }
        else{
        trackSpeed=0;
        }
        currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, trackSpeed);
        }
        else if(currentSpeed < minSpeed){
        currentSpeed += 1 * Time.deltaTime;
        trackSpeed = 0;
        currentSpeed = Mathf.Clamp(currentSpeed, 0, maxSpeed);
        }
        else{
          currentSpeed += maxAcceleration * Time.deltaTime;
          currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
         }
     }
     if(Input.GetKey("s")){
      if(currentSpeed <= minSpeed){
      
      }
      if(gear > 0 ){
      currentSpeed -= maxAcceleration * Time.deltaTime;
          currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed); 
      }
     }
     if(currentSpeed > maxSpeed ){
     }
     if(gear == 0){
  if(Input.GetKey(&quot;r&quot;)){
      currentSpeed -= 200 * Time.deltaTime;
      currentSpeed = Mathf.Clamp(currentSpeed,-10,20);
      transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
      }
  }
  currentSpeed -= 2 * Time.deltaTime;
  currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
 }

when i started the program there is no error, but when i hit the cube there is error like :

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

hopefully someone know what i need. thx you very much.

but if i put funtion Update(){ code here } the program not work at all.

Comment
Add comment · Show 3
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 fadilRaditya · Jul 27, 2012 at 04:48 AM 0
Share

done. any solution?

avatar image AlucardJay · Jul 27, 2012 at 05:32 AM 0
Share

so the error is falling at

 if(data.currentSpeed < 5){

? If so, then this part is not loading the script :

 data = gameObject.GetComponent("manualscar");

either the script is not called manualscar, or it is not attached to the gameObject, therefore it is not found ( NullReferenceException: Object reference not set to an instance of an object )

avatar image fadilRaditya · Jul 27, 2012 at 06:45 AM 0
Share

how to call it? i already read other problem and their solve.. how about $$anonymous$$e? i already set it to person.

1 Reply

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

Answer by Seth-Bergman · Jul 27, 2012 at 07:35 AM

 var data : manualscar;
 
   function Start(){
     data = gameObject.GetComponent(manualscar);
     }

no quotes, this way it returns the type of manualscar, otherwise it returns an Object, which would need to be typecast

EDIT: put it in the start function, and it'll work

second EDIT: you still need to DECLARE the var outside though, as above

next EDIT: if you need to find the script on another object, do this:

   var data : manualscar;
     
       function Start(){
        var player = GameObject.FindWithTag("Player");
         data = player.GetComponent(manualscar);
         }

make sure the tag matches, and this should do it;)

Comment
Add comment · Show 13 · 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 fadilRaditya · Jul 27, 2012 at 07:46 AM 0
Share

still got same error...

avatar image Seth-Bergman · Jul 27, 2012 at 09:02 AM 0
Share

It needs to be inside a function .. see my edit above :)

avatar image fadilRaditya · Jul 27, 2012 at 09:08 AM 0
Share

function Start(){ var data : manualscar; data = gameObject.GetComponent(manualscar); }

function OnTriggerEnter(other : Collider){ if(other.tag == "Player"){ print(data.currentSpeed); } }

i put like that and diffrent error. Assets/script/trackSpeed.js(38,23): BCE0005: $$anonymous$$ identifier: 'data'.

avatar image Seth-Bergman · Jul 27, 2012 at 09:23 AM 0
Share

that's on line number 38 of a script called "trackSpeed".. check the script trackSpeed and see what is on line 38, you are referring to a var which doesn't exist in that script(or it isn't within scope, like if you declared it inside a different function)

avatar image fadilRaditya · Jul 27, 2012 at 09:27 AM 0
Share

trackspeed .js is the name of the file. that is the print data.currentSpeed itselft

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

6 People are following this question.

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

Related Questions

Null Reference Exception Error. Modified Stats. Need Help. 1 Answer

NullReferenceException problem 2 Answers

NullReferenceException 1 Answer

3d Platform Tutorial Help 1 Answer

Error putting an object into an Array 0 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