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 DangerousBeans24 · Jun 11, 2015 at 08:57 AM · errorinputclick to move

Input Issue

Just to warn you i am completely new to scripting, to bear with me here.

So I am trying to build some mechanics for a 3D top down RPG with click to move/attack elements. I'm trying to get some mechanics down so I started with a click to move and a right click to auto attack. creating the left click to move script turned out fine, if a little floaty, but trying to create a right click to auto attack has me stumped. whenever I try to create an input for the right mouse button, I get an error that says, "The name 'input' does not exist in the current context," while when i used the same thing for click to move, it worked fine.

here is my click to move script:

 using UnityEngine;
 using System.Collections;
 
 public class ClicktoMove : MonoBehaviour 
 {
     // the navmesh
     NavMeshAgent agent;
 
     //calls the navmesh
     void Start ()
     {
         agent = GetComponent<NavMeshAgent>();
     }
     
     void Update () 
     {
         Move ();
     }
 
     //Player Movement Method
     //left click to move
     void Move()
     {
         if (Input.GetMouseButtonUp (0)) 
         {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
             
             if (Physics.Raycast (ray, out hit, 100)) 
             {
                 agent.SetDestination (hit.point);
             }
         }
     }
 }

and here is my auto attack. I stopped when the error appeared, so there isn't an action associated with my if statement (yet).

 using UnityEngine;
 using System.Collections;
 
 public class PlayerStats : MonoBehaviour 
 {
     // Player's Stats
     //--------------------------------
     public string name = "Korhol";
 
     public int health;
     
     public int damage;
     
     public float range;
     
     //public float attackSpeed;(not in use)
     //--------------------------------
     // Enemy Script
     //public transform opponent;(not in use)
     public EnemyStats opponent;
     //--------------------------------
     //end variables
 
     void Start () 
     {
         
     }
     
     void Update () 
     {
 
     }
     
     void autoAttack()
     {
         if (Input.getMouseButtonUp(1)) //<----- problem child
         {
 
         }
     }
 }

i would apreciate it if someone could help me out with this and explain why it worked of my moving script but not my attacking script

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by The_Toller · Jun 11, 2015 at 09:33 AM

You're problem is that you're trying to call the getMouseButtonUp method from outside the update function.

"You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has pressed the mouse button and released it again. button values are 0 for left button, 1 for right button, 2 for the middle button." -Unity scripting API

You have set the check for the mouse event in a method you never call and you need it in the update function! :) Try this solution instead and you'll be fine!

 using UnityEngine;
 using System.Collections;

 public class PlayerStats : MonoBehaviour 
 {
     // Player's Stats
     //--------------------------------
     public string name = "Korhol";
     
     public int health;
     
     public int damage;
     
     public float range;
     
     //public float attackSpeed;(not in use)
     //--------------------------------
     // Enemy Script
     //public transform opponent;(not in use)
     public EnemyStats opponent;
     //--------------------------------
     //end variables
     
     void Start () 
     {
         
     }
     
     void Update () 
     {
         if(Input.GetMouseButtonUp(1))
         {
             AutoAttack();
         }
 
     }
     
     void AutoAttack()
     {
 
     }
 }

In your first script, you're calling your input check method from the update function. However, you're not doing that in your PlayerStats. This solution is more like your first script and would also work:

     void Update () 
     {
             AutoAttack();
     }
     
     void AutoAttack()
     {
         if(Input.GetMouseButtonUp(1))
         {
 
         }
     }
 }

Have a nice day, and good luck with your game!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Input button is not setup?? 2 Answers

Input manager cross platform 2 Answers

UnityException: Input Axis Rotate2 is not setup. 0 Answers

UFPS + Adventure Creator (Input Error) 1 Answer

Input.GetKeyDown doesn't work first button press 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