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 NerdRageStudios · Jul 11, 2014 at 08:22 PM · c#coroutinereferenceinstance

Object reference not set to an instance of an object

Hi, I have had a working script in JS that controls my character, but I am trying to re-write it in C#.

It does not error until I try to jump using the playerOneJumpTime routine, then I get the object reference error.

I have gone through the code over and over and I cant see why the jump coroutine is causing problems?

Please can someone help as I cant see the woods for the trees anymore.

 using UnityEngine;
 using System.Collections;
 
     [System.Serializable]
     public class playerOneAttributes
     {
     public string playerOneName = "Player 1";                //Players name in game, can be changed
     public int playerOneHealth = 100;                        //Players Total Current Health
     public int playerOneRangePower = 100;                    //Players range damage (ie. fireballs, arrows)
     public int playerOneMagicDamage = 100;                    //Players spell damage from potions
     public int playerOneMeleeDamage = 100;                    //Players melee damage
     public int playerOneDefense = 10;                        //Players Defense
     public float playerOneMoveSpeed = 100;                    //Players movement speed
     public int playerOneXP = 0;                                //Players XP
     }
 
     [System.Serializable]
     public class playerOnelevelUpMultipliers
     {
         public float playerOneHealthMultiplier = 1.5f;          //Players health multplier upon level up
         public float playerOneRangeMultiplier = 1.5f;           //Players range power multplier upon level up
         public float playerOneMagicMultiplier = 1.5f;           //Players magic power multplier upon level up
         public float playerOneMeleeMultiplier = 1.5f;           //Players melee power multplier upon level up
         public float playerOneDefenseMultiplier = 1.5f;         //Players defense multplier upon level up
     }
 
     [System.Serializable]
     public class playerOneControls
     {
         public string playerOneControlUp = "w";                    //Player Controls (re-mappable)
         public string playerOneControlDown = "s";                //Player Controls (re-mappable)
         public string playerOneControlLeft = "a";                //Player Controls (re-mappable)
         public string playerOneControlRight = "d";                //Player Controls (re-mappable)
         public string playerOneControlJump = ",";                //Player Controls (re-mappable)
         public string playerOneControlAttack = ".";                //Player Controls (re-mappable)
         public string playerOneControlMagic = "/";                //Player Controls (re-mappable)
     }
 
     public class playerOnePhysics
     {
         public float playerOneJumpRate = 1.1f;                    //Players time between jumps
         public float playerOneJumpPower = 5.0f;                    //Players Jump Power
     }
 
     [System.Serializable]
     public class playerOneStatus
     {
         public bool playerOneAlive = true;                        //Is player one alive?
         public bool playerOneIsIt = false;                        //Is player IT?
         public bool playerOneStunned = false;                   //Is player one stunned?
         public bool playerOnePoisoned = false;                    //Is player one poisoned?
     }
 
     [System.Serializable]
     public class playerOneInventory
     {
         public int playerOnePotionAmount = 0;                    //Magic potion amount
         public int playerOneKeyAmount = 0;                        //Number of keys held
         public bool playerOnePowerUpOne = false;                //Extra Shot Power
         public bool playerOnePowerUpTwo = false;                //Extra Melee Power
         public bool playerOnePowerUpThree = false;                //Extra Magic Power
         public bool playerOnePowerUpFour = false;                //Extra Speed
         public bool playerOnePowerUpFive = false;                //Extra Armour
         public bool playerOnePowerUpSix = false;                //Extra Fight Power
         public bool playerOnePowerUpSeven = false;                //Extra XP
     }
 
     public class ScriptPlayerOneManager : MonoBehaviour
     {
 
         public playerOneAttributes playerAttributes;            //These show the variables in the inspector
         public playerOnelevelUpMultipliers levelUpMultipliers;
         public playerOneControls playerControls;
         public playerOnePhysics playerPhysics;
         public playerOneStatus playerStatus;
         public playerOneInventory playerInventory;
         
 
         // Use this for initialization
         void Start()
         {
             rigidbody.freezeRotation = true;                // Prevent the player from falling over due to physics
             StartCoroutine(playerOneJumpTime());
             
         }
 
 
         // Update is called once per frame
         void Update()
             {
             if (Input.GetKey(playerControls.playerOneControlUp))
             {
                 transform.position += new Vector3(0, 0, 0.035f * playerAttributes.playerOneMoveSpeed) * Time.deltaTime;
             }
 
             if (Input.GetKey(playerControls.playerOneControlDown))
             {
                 transform.position -= new Vector3(0, 0, 0.035f * playerAttributes.playerOneMoveSpeed) * Time.deltaTime;
             }
 
             if (Input.GetKey(playerControls.playerOneControlRight))
             {
                 transform.position += new Vector3(0.035f * playerAttributes.playerOneMoveSpeed, 0, 0) * Time.deltaTime;
             }
 
             if (Input.GetKey(playerControls.playerOneControlLeft))
             {
                 transform.position -= new Vector3(0.035f * playerAttributes.playerOneMoveSpeed, 0, 0) * Time.deltaTime;
             }
 
           
         }
 
         IEnumerator playerOneJumpTime()        // This function controls the players jump - makes the player wait to be able to jump again
         {
             while (true)
             {
                 if (Input.GetKeyDown(playerControls.playerOneControlJump))
                 {
                     rigidbody.velocity = new Vector3(0, playerPhysics.playerOneJumpPower, 0);
                     yield return new WaitForSeconds(playerPhysics.playerOneJumpRate);
                 }
                 else
                 {
                     yield return null;
                 }
             }
         }
 
 
     }
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 Firedan1176 · Jul 11, 2014 at 08:28 PM 0
Share

Are you getting an error on line 120? Use if statements and/or debug logs to see if it's there (something like this:)

 if (!rigidbody) {
 Debug.Log("Freak out!");
 }
avatar image NerdRageStudios · Jul 11, 2014 at 08:41 PM 0
Share

Now that I have done what Landem suggested, it all works fine with no errors :)

1 Reply

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

Answer by Landern · Jul 11, 2014 at 08:27 PM

playerPhysics is not instantiated in class ScriptPlayerOneManager

In the public field instantaite it and others:

 public playerOnePhysics playerPhysics = new playerOnePhysics();

Please PLEASE notice that none of your other fields in ScriptPlayerOneManager are instantiated meaning you will get a null reference from each of them as you try and access it's members.

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 NerdRageStudios · Jul 11, 2014 at 08:30 PM 0
Share

Oh my goodness, you are a legend!

thank you :)

I really am having a hard time going from JS to c#!

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to shortcut a lambda expression through a function? 1 Answer

Trouble creating reference of image in script. 1 Answer

Easily reference class instance 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