Search Unity

Question Help me i joined unity 4 months ago and i dont know how to fix the error CS0138:

Discussion in 'Scripting' started by DashingMortis, Jun 1, 2020.

  1. DashingMortis

    DashingMortis

    Joined:
    Jun 1, 2020
    Posts:
    13
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.Debug;
    3. using UnityEngine;
    4.  
    5. public class JoyStickAttack : MonoBehaviour
    6. {
    7.     private Vector2 startingPoint;
    8.     private int leftTouch = 99;
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         //  Identifying if the button was just pressed or pressed or not pressed
    14.         int i = 0;
    15.         while (i < Input.TouchCount)
    16.             Touch t = Input.GetTouch(i);
    17.             Vector2 touchPos = getTouchPosition(t.position) * -1;
    18.             if(t.phase == TouchPhase.Began)
    19.             {
    20.                  if(t.position.x > ScreenWidth / 2)
    21.                  {
    22.                      shootBullet();
    23.                  }
    24.                  else
    25.                  {
    26.                 leftTouch = t.fingerId;
    27.                 startingPoint = touchPos;
    28.                  }
    29.  
    30.             }else if(t.phase == TouchPhase.Moved)
    31.             {
    32.  
    33.             }else if(t.phase == TouchPhase.Ended)
    34.             {
    35.  
    36.             }
    37.     }
    38.  
    39.     //Identifyies where is the finger touching
    40.     Vector2 getTouchPosition(Vector2 touchPosition)
    41.     {
    42.         return GetComponent<Camera>().ScreenToWorldPoint(new Vector3(touchPosition.x, touchPostion.y, transform.position.z));
    43.     }
    44.  
    45.  
    46.  
    47.  
    48.     void CharacterMovement(Vector2 direction)
    49.     {
    50.        
    51.     }
    52.  
    53.     void shootBullet()
    54.     {
    55.        
    56.     }
    57. }
    58.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Get rid of
    using UnityEngine.Debug
    .
     
  3. DashingMortis

    DashingMortis

    Joined:
    Jun 1, 2020
    Posts:
    13
    the error is:
    error CS0138: A 'using namespace' directive can only be applied to namespaces; 'Debug' is a type not a namespace. Consider a 'using static' directive instead
     
  4. DashingMortis

    DashingMortis

    Joined:
    Jun 1, 2020
    Posts:
    13
    i tried it gave me 5 new errors
     
  5. DashingMortis

    DashingMortis

    Joined:
    Jun 1, 2020
    Posts:
    13
    it gave me the errors
    error CS0117: 'Input' does not contain a definition for 'TouchCount'
    error CS1023: Embedded statement cannot be a declaration or labeled statement
    error CS0103: The name 't' does not exist in the current context
    The name 'ScreenWidth' does not exist in the current context
    error CS0103: The name 'touchPostion' does not exist in the current context
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Should be "touchCount", with a lowercase 't'.
    https://docs.unity3d.com/ScriptReference/Input-touchCount.html
    You're missing curly braces in your while-loop statement.
    It looks like you're also creating an endless loop as well, since you're not incrementing the
    i
    variable that the while-loop's condition is checking.

    Why not use a for-loop instead?
    Code (CSharp):
    1. for(int i = 0; i < Input.touchCount; i++)
    2. {
    3.    Touch t = Input.GetTouch(i);
    4.    //etc...
    5. }
    You haven't declared a "ScreenWidth" variable anywhere in your script. You're also using a "TouchPhase" variable, but this variable hasn't been declared anywhere either.
    You misspelled "touchPosition".
     
  7. DashingMortis

    DashingMortis

    Joined:
    Jun 1, 2020
    Posts:
    13
    oops btw thanks bro