Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 bryanbas · Jul 16, 2015 at 11:53 AM · c#errornullreferenceexceptionplatformerpowerup

C# - instance is not set to an instance of an object

I am a noob when it comes to c#, but i am getting new errors everytime i post a question about the previous question, so i thought: maybe someone can clean this script up, i am using the caracter motor from the standard assets and i want to change the value of jumpHeight because one platform has a powerup but that platform is higher than the players origional jump, so the player needs a powerup with this script, but this script doesn't work, i think the problem lies in the 'caracterMotor motor;' line, so my question is: can someone PLEASE fix this? thank you!

 using UnityEngine;
  using System.Collections;
 
    public class jumpUp : MonoBehaviour 
    {
        private CharacterMotor motor;
        public GameObject player;
        public bool enable;
        public bool destroy;
 
        void start(){
            motor = player.GetComponent<CharacterMotor>();
            bool enable = true;
            bool destroy = true;
        }
        
        void  OnTriggerEnter(Collider other)
      {
 
         
         if(enable == true){
            motor.jumping.baseHeight = 2.0f;
            Destroy(gameObject);
         }
         else
         {
            motor.jumping.baseHeight = 1.0f;
            Destroy(gameObject);
         }
           
         }
      }




Comment
Add comment · Show 2
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 InfiniBuzz · Jul 16, 2015 at 11:52 AM 0
Share

What does the NullReferenceException exactly say? I assume it happens because your motor variable is null and you never assign it. $$anonymous$$ake it public and assign it in the Inspector or get the reference in a Start() function. The two if statements at the start of your OnTriggerEnter function don't make sense since you set the variable to true right before (on declaration). Also line 15 and the following line say if enable is true -> if enable is false then... which is not possible. The codeblock on line 32 is empty. I suggest to start with tutorials if you are new. Another tip is if you happen to have an error/not working script, go trough the script step by step and think about what you want it to happen and what actually happens. This helps to avoid errors in your logic.

avatar image bryanbas · Jul 16, 2015 at 12:59 PM 0
Share

it says: instance is not set to an isnstance of an object... i edited my post to my code now, however, still, when i collide with the powerup, it gives me that error

2 Replies

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

Answer by Ibzy · Jul 16, 2015 at 11:57 AM

You need to reference the relevant components. Currently you are creating null variables and not assigning them a value.

Create a void Start() function and, providing this script is attached to the player, use motor = GetComponent<CharacterMotor>();

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 bryanbas · Jul 16, 2015 at 12:36 PM 0
Share

no it is on the powerup not on the player

avatar image Piflik · Jul 16, 2015 at 12:59 PM 1
Share

If this is on the powerup, then you have to get the player-object's charactermotor component.

 void OnTriggerEnter(Collider other) {
     Character$$anonymous$$otor motor = other.GetComponent<Character$$anonymous$$otor>();
     if(motor != null) {
         //now you can access public members of the player's motor script
     }
 
 }

avatar image bryanbas · Jul 16, 2015 at 01:09 PM 0
Share

THAN$$anonymous$$ YOU SO FREA$$anonymous$$IN $$anonymous$$UCH THAN$$anonymous$$ YOU!!!!!!!!!!!

avatar image bryanbas · Jul 16, 2015 at 01:14 PM 0
Share

piflik i have put you in the thankyou file of the game

avatar image
0

Answer by Hexer · Jul 16, 2015 at 12:57 PM

you dont need to reference or assign CharacterMotor in your script. From my point of view it is confusing. I would have make the jumping.baseHeight a static variable.

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics

Edit: oops pasted wrong link(fixed).

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException error in an array of objects 0 Answers

Im getting a "NullReferenceException: Object reference not set to an instance of an object" error not sure why. 1 Answer

NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 1 Answer

2D platformer- getting errors I don't understand (c#) 1 Answer

Can't find source of trigger Null Reference Exception 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