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 DubstepDragon · Feb 21, 2014 at 10:38 PM · c#2derrorbuttonmove

Something wrong with my movement script...

Just a quick point-out: This script SHOULD have bools to move, I did not do it out of lack of programming experience. I did this so I can access the movement from other scripts whilst still having it move... However it seems like it is due to my lack of programming experience, otherwise I wouldn't be here seeking the wisdom of your beautiful gray-matter.

Something is wrong, it is not moving. Can someone assist me? Or perhaps suggest a better approach? Thanks in advance :D

Here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
 
     public static bool IsAlive = true;
 
     public static bool isMovingRight = false;
     public static bool isMovingLeft = false;
 
     public float speedVar;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetKey(KeyCode.A)) {
             isMovingLeft = true;
         }
 
         if(Input.GetKey(KeyCode.D)) {
             isMovingRight = true;
         }
 
         //SWAGDIVIDERALLHAILLORDSWAGAXXUSPLZNOMALFURIONSPAMMANYLEGENDARYDOGECATELOGE
 
         if(isMovingLeft = true) {
             transform.Translate(Vector3.left * Time.deltaTime * speedVar);
         }
 
         if(isMovingRight = true) {
             transform.Translate(Vector3.right * Time.deltaTime * speedVar);
         }
 
         if (transform.position.x > 5) {
             transform.position = new Vector2(5, transform.position.y); // can't go further than 5
         }
 
         else if (transform.position.x < -5){
             transform.position = new Vector2(-5, transform.position.y); // can't go further than -5
         }
 
     }
 
     void OnCollisionEnter(Collision other) {
         if(other.gameObject.CompareTag("Kill")) {
             Death();
             Destroy(this.gameObject);
         }
     }
 
     void Death() {
         IsAlive = false;
         PlayerPrefs.SetInt ("Score", Score.scoreValue);
     }
 }



Thanks so much you magnificent community :D

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

2 Replies

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

Answer by VioKyma · Feb 21, 2014 at 10:52 PM

Something that immediately sticks out is on line 31 and 35, you have:

 if(isMovingLeft = true)

AND

 if(isMovingRight = true)

Both of these statements will always evaluate to true because you are using assignment (single equals sign), so instead use the following:

 if(isMovingLeft)

AND

 if(isMovingRight)

Note: you could also use == true to evaluate a comparison.

Hope this helps!

Comment
Add comment · Show 6 · 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 DubstepDragon · Feb 21, 2014 at 11:48 PM 0
Share

There's still a problem...

When I move, he only moves left, not right...

Additionally, the GUI buttons I have set up to take care of movements don't work as well...

Here is my GUI script:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$oveButtons : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void OnGUI () {
         if(GUI.Button(new Rect(Screen.width - Screen.width, Screen.height - Screen.height, 300, Screen.height), "LEFT")) {
             $$anonymous$$ovement.is$$anonymous$$ovingLeft = true;
         }
 
         if(GUI.Button(new Rect(Screen.width - 300, Screen.height - Screen.height, 300, Screen.height), "RIGHT")) {
             $$anonymous$$ovement.is$$anonymous$$ovingRight = true;
         }
     }
 }


Hope that helps :D

avatar image DubstepDragon · Feb 21, 2014 at 11:49 PM 0
Share

and if I remove the "= true" as you told me, it returns an error :'(

avatar image VioKyma · Feb 22, 2014 at 01:31 AM 0
Share

What is the error you get when you remove '= true'?

avatar image VioKyma · Feb 22, 2014 at 01:52 AM 0
Share

With the GUI buttons, you need to reference an actual instance of $$anonymous$$ovement, not the script itself. You have two options here, you can either pass the object the $$anonymous$$ovement script is attached to, or you can create and use a Singleton pattern.

For the first option, your script would look something like:

     using UnityEngine;
     using System.Collections;
      
     public class $$anonymous$$oveButtons : $$anonymous$$onoBehaviour {
      
     public GameObject movementObject;
     
     // Use this for initialization
     void Start () {
 
     }
      
     // Update is called once per frame
     void OnGUI () {
         $$anonymous$$ovement moveScript = movementObject.GetComponent<$$anonymous$$ovement>();    
 
         if(GUI.Button(new Rect(Screen.width - Screen.width, Screen.height - Screen.height, 300, Screen.height), "LEFT")) {
             moveScript.is$$anonymous$$ovingLeft = true;
         }
      
         if(GUI.Button(new Rect(Screen.width - 300, Screen.height - Screen.height, 300, Screen.height), "RIGHT")) {
             moveScript.is$$anonymous$$ovingRight = true;
         }
     }
     }

With the second, it's a bit more complicated, but check out this link here for a basic implementation: http://wiki.unity3d.com/index.php/Singleton

avatar image DubstepDragon · Feb 22, 2014 at 08:50 PM 0
Share

I am now getting the following errors:

  • Assets/$$anonymous$$oveButtons.cs(18,36): error CS0176: Static member $$anonymous$$ovement.is$$anonymous$$ovingLeft' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d - Assets/$$anonymous$$oveButtons.cs(22,36): error CS0176: Static member $$anonymous$$ovement.is$$anonymous$$ovingRight' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d

I do not know the cause of this issue, please reply asap. Thank you so much for your help thus far :D

Show more comments
avatar image
1

Answer by j_perker · Feb 21, 2014 at 11:07 PM

You used a =, not == to check for the condition silly.

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 DubstepDragon · Feb 21, 2014 at 11:45 PM 0
Share

Thanks bro :D

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

20 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

Related Questions

My Function is not showing up in the OnClick Menu 2017.2.0f 0 Answers

Keep getting this error CS8025 1 Answer

Space opening main menu (undesired) 0 Answers

i auto generated a input manager c# script like Brackey's video and i am now getting compiler errors even though i did the exact same thing as him. Very confused. 2 Answers

2D platformer- getting errors I don't understand (c#) 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