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 Sgt_Spike · Jul 11, 2018 at 09:36 PM · errorbuttoncoroutinenullreferenceexceptioninteractable

Setting button intractability causes an error

Hi everyone. I'm getting very annoyed and tired of this now as I've been looking at this for 3 hours and still haven't solved it. Here is the error I'm getting 'NullReferenceException: Object reference not set to an instance of an object PlayerMovement+c__Iterator8.MoveNext () (at Assets/Scripts/PlayerMovement.cs:280) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) PlayerMovement:Update() (at Assets/Scripts/PlayerMovement.cs:54)' Quite long isn't it? I have no idea why I'm getting it though. This here is the PlayerMovement script where the error points to. public class PlayerMovement : MonoBehaviour {

     public Camera cameraScript;
 
     public AudioSource playerMovement;
 
     public Button greenArrow;
     public Button blueArrow;
     public Button yellowArrow;
     public Button redArrow;
 
     public float pos;
     private float rot;
 
     private float x;
     private float z;
 
     public static bool playerMoved;
     public static string lastMoveDir;
     public static string changeYBy;
 
     public static bool canMove;
 
     public static int moves;
     public Text movesText;
 
     private void Start()
     {
         moves = 0;
         playerMoved = false;
 
         canMove = true;
 
         pos = 0;
         rot = 0;
 
     }
 
     private void Update()
     {
         transform.eulerAngles = new Vector3(x, transform.rotation.y, z);
         if (movesText != null)
         {
             movesText.text = moves.ToString();
         }
 
         if(changeYBy == "Up")
         {
             StartCoroutine(MoveYUp());
             changeYBy = "";
         }
         if (changeYBy == "Down")
         {
             StartCoroutine(MoveYDown());
             changeYBy = "";
         }
     }
 
     private void OnTriggerEnter(Collider col)
     {
         if (col.tag == "Chapter1Bullet")
         {
             Chapter1Boss.hitPlayer = true;
         }
     }
 
     public void LeftButton()
     {
         if (GameMaster.endTheLevel == false && canMove == true && Environment.environmentReady == true)
         {
             moves = moves + 1;
             playerMoved = true;
    
             StartCoroutine(MoveLeft());
             StartCoroutine(RotateLeft());
             lastMoveDir = "Left";
         }
         
     }
     public void RightButton()
     {
         if (GameMaster.endTheLevel == false && canMove == true && Environment.environmentReady == true)
         {
             moves = moves + 1;
             playerMoved = true;
 
             StartCoroutine(MoveRight());
             StartCoroutine(RotateRight());
             lastMoveDir = "Right";
         }
         
     }
     public void UpButton()
     {
         if (GameMaster.endTheLevel == false && canMove == true && Environment.environmentReady == true)
         {
             moves = moves + 1;
             playerMoved = true;
 
             StartCoroutine(MoveUp());
             StartCoroutine(RotateUp());
             lastMoveDir = "Up";
         }
         
     }
     public void DownButton()
     {
         if (GameMaster.endTheLevel == false && canMove == true && Environment.environmentReady == true)
         {
             moves = moves + 1;
             playerMoved = true;
 
             StartCoroutine(MoveDown());
             StartCoroutine(RotateDown());
             lastMoveDir = "Down";
         }
         
     }
 
     IEnumerator MoveLeft()
     {
         Camera.enableArrows = false;
         greenArrow.interactable = false;
         blueArrow.interactable = false;
         yellowArrow.interactable = false;
         redArrow.interactable = false;
 
         pos = this.transform.position.z - 0.4f;
         while (this.transform.position.z > pos)
         {
             this.transform.position = this.transform.position + new Vector3(0f, 0f, -0.06f);
             yield return new WaitForSeconds(0.005f);
         }
         this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, pos);
         pos = 0;
 
         yield return new WaitForSeconds(0.05f);
         Camera.enableArrows = true;
         greenArrow.interactable = true;
         blueArrow.interactable = true;
         yellowArrow.interactable = true;
         redArrow.interactable = true;
     }
     IEnumerator RotateLeft()
     {
         playerMovement.Play();
         z = 0;
         rot = x - 90f;
         while (x > rot)
         {
             x = x - 6f;
             yield return new WaitForSeconds(0.007f);
         }
         
         x = rot;
         rot = 0;
     }
     IEnumerator MoveUp()
     {
         Camera.enableArrows = false;
         greenArrow.interactable = false;
         blueArrow.interactable = false;
         yellowArrow.interactable = false;
         redArrow.interactable = false;
 
         pos = this.transform.position.x - 0.4f;
         while (this.transform.position.x > pos)
         {
             this.transform.position = this.transform.position + new Vector3(-0.06f, 0f, 0f);
             yield return new WaitForSeconds(0.005f);
         }
         this.transform.position = new Vector3(pos, this.transform.position.y, this.transform.position.z);
         pos = 0;
 
         yield return new WaitForSeconds(0.05f);
         Camera.enableArrows = true;
         greenArrow.interactable = true;
         blueArrow.interactable = true;
         yellowArrow.interactable = true;
         redArrow.interactable = true;
     }
     IEnumerator RotateUp()
     {
         playerMovement.Play();
         x = 0;
         rot = z + 90f;
         while (z < rot)
         {
             z = z + 6f;
             yield return new WaitForSeconds(0.007f);
         }
         
         z = rot;
         rot = 0;
     }
     IEnumerator MoveRight()
     {
         Camera.enableArrows = false;
         greenArrow.interactable = false;
         blueArrow.interactable = false;
         yellowArrow.interactable = false;
         redArrow.interactable = false;
 
         pos = this.transform.position.z + 0.4f;
         while (this.transform.position.z < pos)
         {
             this.transform.position = this.transform.position + new Vector3(0f, 0f, 0.06f);
             yield return new WaitForSeconds(0.005f);
         }
         this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, pos);
         pos = 0;
 
         yield return new WaitForSeconds(0.05f);
         Camera.enableArrows = true;
         greenArrow.interactable = true;
         blueArrow.interactable = true;
         yellowArrow.interactable = true;
         redArrow.interactable = true;
     }
     IEnumerator RotateRight()
     {
         playerMovement.Play();
         z = 0;
         rot = x + 90f;
         while (x < rot)
         {
             x = x + 6f;
             yield return new WaitForSeconds(0.007f);
         }
         
         x = rot;
         rot = 0;
     }
     IEnumerator MoveDown()
     {
         Camera.enableArrows = false;
         greenArrow.interactable = false;
         blueArrow.interactable = false;
         yellowArrow.interactable = false;
         redArrow.interactable = false;
 
         pos = this.transform.position.x + 0.4f;
         while (this.transform.position.x < pos)
         {
             this.transform.position = this.transform.position + new Vector3(0.06f, 0f, 0f);
             yield return new WaitForSeconds(0.005f);
         }
         this.transform.position = new Vector3(pos, this.transform.position.y, this.transform.position.z);
         pos = 0;
 
         yield return new WaitForSeconds(0.05f);
         Camera.enableArrows = true;
         greenArrow.interactable = true;
         blueArrow.interactable = true;
         yellowArrow.interactable = true;
         redArrow.interactable = true;
     }
     IEnumerator RotateDown()
     {
         playerMovement.Play();
         x = 0;
         rot = z - 90f;
         while (z > rot)
         {
             z = z - 6f;
             yield return new WaitForSeconds(0.007f);
         }
         
         z = rot;
         rot = 0;
     }
     IEnumerator MoveYUp()
     {
         Camera.enableArrows = false;
         greenArrow.interactable = false;
         blueArrow.interactable = false;
         yellowArrow.interactable = false;
         redArrow.interactable = false;
 
         pos = this.transform.position.y + 0.4f;
 
         while (this.transform.position.y < pos)
         {
             this.transform.position = this.transform.position + new Vector3(0f, 0.06f, 0f);
             yield return new WaitForSeconds(0.005f);
         }
         this.transform.position = new Vector3(this.transform.position.x, pos, this.transform.position.z);
         pos = 0;
 
         yield return new WaitForSeconds(0.05f);
 
         if (lastMoveDir == "Left")
         {
             LeftButton();
         }
         if (lastMoveDir == "Right")
         {
             RightButton();
         }
         if (lastMoveDir == "Up")
         {
             UpButton();
         }
         if (lastMoveDir == "Down")
         {
             DownButton();
         }
     }
     IEnumerator MoveYDown()
     {
         Camera.enableArrows = false;
         greenArrow.interactable = false;
         blueArrow.interactable = false;
         yellowArrow.interactable = false;
         redArrow.interactable = false;
 
         pos = this.transform.position.y - 0.4f;
         while (this.transform.position.y > pos)
         {
             this.transform.position = this.transform.position - new Vector3(0f, 0.06f, 0f);
             yield return new WaitForSeconds(0.005f);
         }
         this.transform.position = new Vector3(this.transform.position.x, pos, this.transform.position.z);
         pos = 0;
 
         yield return new WaitForSeconds(0.05f);
         Camera.enableArrows = true;
         greenArrow.interactable = true;
         blueArrow.interactable = true;
         yellowArrow.interactable = true;
         redArrow.interactable = true;
     }
 }

The error appears on line 280, which is the 'greenArrow.interactable = false;' inside the MoveYUp coroutine. greenArrow and the others are buttons, so why does this throw an error. And yes, the buttons are linked up correctly in Unity.

Also the weird thing is is that the error only starts appearing in these buttons when the 'MoveYUp' coroutine is started. Everything is fine before that.

If someone could give me some advice before I quit Unity for life that would be great lol :D

Thanks everyone!

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 NoDumbQuestion · Jul 12, 2018 at 01:39 AM 0
Share
   Camera.enableArrows = false;

Did you make another script name as "Camera"? $$anonymous$$aybe unity compiler mistook it as UnityEngine.Camera and not yourScript.Camera

avatar image Sgt_Spike NoDumbQuestion · Jul 12, 2018 at 01:12 PM 0
Share

Thanks for the answer! I do have another script called camera. I have checked to make sure that it isn't UnityEngine.Camera and I'm correct. It is linked to the class so that isn't the issue sadly...

1 Reply

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

Answer by Sgt_Spike · Jul 12, 2018 at 01:29 PM

Right, so I found the issue. Turns out the answer is very simple. I had the PlayerMovement script attached to another object aswell which had nothing referenced. That would explain the audiosource error as well as the cameraScript error.

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

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

Related Questions

There's an issue with this script I can't find 0 Answers

Null Reference in script after having called other class without a null reference 0 Answers

Failed to call function "" of class "". Calling function "" with no parameters but the function requires 1 2 Answers

JNI call block Coroutine & update thread 0 Answers

NullReferenceException: Object reference not set to an instance of an object ... Why? 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