error CS0138: A `using' directive can only be applied to namespaces but `UnityEngine.Touch' denotes

Error: A using' directive can only be applied to namespaces but UnityEngine.Touch’ denotes a type. Consider using a `using static’ instead

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Touch;


public class DragControll : MonoBehaviour
{
    private float deltaX, deltaY , deltaZ;
    private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);

            switch (touch.phase)
            {
                case TouchPhase.Began:
                    deltaX = touchPos.x - transform.position.x;
                    deltaY = touchPos.y - transform.position.y;
                    deltaZ = touchPos.z - transform.position.z;

                    break;

                case TouchPhase.Moved:
                    rb.MovePosition(new Vector2(touchPos.x - delataX, touchPos.y - deltaY , touchPos.z - deltaZ));
                    break;

                case TouchPhase.Ended:
                    rb.velocity = Vector2.zero;
                    break;
            }
        }
    }
}

UnityEngine.Touch is a type, and you’re trying it to use as if it was a namespace. Well, that’s also what the error says. Just remove that line and you should be fine.

There’s a way you can use a type there, and that’s what the message suggests: “using static” (C# 6+ feature) but it’s definitely not needed here.

1 Like

If I remove that line “using UnityEngine.Touch” I would get 3 new errors, obviously I need to use that line.

And what are these errors? I haven’t taken a look at the rest of the script, but you may just post the other errors.

I can tell that this line is certainly not what you need, and less errors do not mean you’ve partially fixed something.

  1. error CS0029: Cannot implicitly convert type UnityEngine.Touch' to Touch’
  2. error CS1061: Type Touch' does not contain a definition for position’ and no extension method position' of type Touch’ could be found. Are you missing an assembly reference?
  3. error CS1061: Type Touch' does not contain a definition for phase’ and no extension method phase' of type Touch’ could be found. Are you missing an assembly reference?

It seems you created a type with name “Touch” in your project, which is - just like your current type DragControll - declared in the global namespace (the default namespace, or “no explicit” namespace). You might as well have imported a plugin or other assets, but good plugins usually come with their own namespaces, so I guess it’s just somewhere in your project.

The compiler thinks you want to use that “Touch” in line 21 of your script, rather than UnityEngine.Touch, so the compiler sees that line as trying to treat an apple (a “UnityEngine.Touch”) like an orange (a “Touch”).

If you’re using Visual Studio, click on that “Touch” and hit F12. It’ll jump to the definition of that type. Other IDEs should have a similar “go to defintiion” functionality.

1 Like

So, what should I do. Some people said that I should replace “Touch” in line 21. , but with what?

Hm yes, that’s what you could do - but it only solves the problem in this specific case, and doesn’t really fix that annoying issue once and forever.

The replacement would just be “Touch” => “UnityEngine.Touch” in order to be more explicit, so that the compiler knows that you actually want the engine’s touch type. The shorter version is based on type inference, which is just a simple “var”.

UnityEngine.Touch touch = ...
// or shorter, allowing to infer the type from the expression on the right side of the assignement operator
var touch = ...

What you should do:
Find that type named “Touch” in your project, and either remove it (if no longer needed), or rename it (if still needed). It’s generally a good practice to avoid type names that are already used in Unity (this does actually apply to every environment, doesn’t need to be Unity), or at least to wrap your own definitions in your own namespace (it’s always a good idea to use namespace, you’ll immediately know whether some type is yours, or comes from a different source and it helps to organize all your own types).

Sorry for bothering, I’m still learning to work in Unity and VisualStudio. I replaced “Touch” with “var” , but I still get errors, 4 of them. I feel like I should declare a variable on line 11, but what.

error CS1061: Type UnityEngine.Vector2' does not contain a definition for z’ and no extension method z' of type UnityEngine.Vector2’ could be found. Are you missing an assembly reference?

error CS0103: The name `delataX’ does not exist in the current context

error CS1061: Type UnityEngine.Vector2' does not contain a definition for z’ and no extension method z' of type UnityEngine.Vector2’ could be found. Are you missing an assembly reference?

error CS1729: The type UnityEngine.Vector2' does not contain a constructor that takes 3’ arguments

Yes, these errors are different ones, and they’re now considered unrelated to the previous errors, hence they do pop-up now.

A Vector2 has only two dimensions, hence the 2 as appendix, ‘z’ being none of them (in Unity). A three-dimensional vector has x, y, z components.

If you intended to use ‘z’, i.e. three dimensions, which you did apparently, at least that’s what the whole code snippet suggests, you should replace Vector2 with Vector3. That should solve these errors.

This one’s just popping up due to your typo. You’ve got a variable with the identifier “deltaX”, but you’ve typed “delataX”.

That fixed everything, thanks for your help!

1 Like

im using the random audio player script from unity 2d plateformer kit
and im using sprites and the code hsa a reference to tilemaps.
so i use " Public Sprite PlateformObject;" and i get errors
about not having using static and when i use the using static unityengine.sprite; i get the error that this isnt supported in c# 4 . please help

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.Sprite;

namespace Gamekit2D
{
[RequireComponent(typeof(AudioSource))]
public class RandomAudioPlayer : MonoBehaviour
{
[System.Serializable]
public struct TileOverride
{
//public TileBase tile;
public Sprite PlateformObject;
public AudioClip[ ] clips;
}

public AudioClip[ ] clips;

public Sprite[ ] overrides;

public bool randomizePitch = false;
public float pitchRange = 0.2f;

protected AudioSource m_Source;
protected Dictionary<Sprite, AudioClip[ ]> m_LookupOverride;

private void Awake()
{
m_Source = GetComponent();
m_LookupOverride = new Dictionary<Sprite, AudioClip[ ]>();

for(int i = 0; i < overrides.Length; ++i)
{
if (overrides*.Sprite == null)*
continue;
m_LookupOverride[overrides_.Sprite] = overrides*.clips;
}
}
public void PlayRandomSound(Sprite surface = null)
{
AudioClip[ ] source = clips;
AudioClip[ ] temp;
if (surface != null && m_LookupOverride.TryGetValue(surface, out temp))
source = temp;
int choice = Random.Range(0, source.Length);
if(randomizePitch)
m_Source.pitch = Random.Range(1.0f - pitchRange, 1.0f + pitchRange);
m_Source.PlayOneShot(source[choice]);
}
public void Stop()
{
m_Source.Stop();
}
}
}*_