is a namespace but is used like a type

Hello.
I can’t seem to correct this error, sorry I’m a beginner.

I have 2 scripts.
The first :

namespace MyScriptA.Track {
using UnityEngine;
using System.Collections;
public class GoSwitch : MonoBehaviour {
public MyTSwitcher onswitch;

public void onActiveLeTruc() // <— how do I run this function from my B script?
{
onswitch.Switch();
}
}
}

the second :

public class MyScriptB: MonoBehaviour
{
void Shoot()
{
MyScriptA.Track GoSwitch = FindObjectOfType<MyScriptA.Track>(); // error : MyScriptA.Track is a namespace but is used like a type
GoSwitch.onActiveLeTruc();
}
}
Thanks for your help

using MyScriptA.Track;

public class MyScriptB: MonoBehaviour
{
    void Shoot()
    {
        var goSwitch = FindObjectOfType<GoSwitch>();
        goSwitch.onActiveLeTruc();
    }
}

Check out what the code formatter can do on the forum posts… use it!

(also fixed the namespace too, you’re welcome)

Thank you, sorry for the bad code formatter

Hi, I have just gotten acquainted with Unity and I am making a 2D game, but it gives me an error that I have not seen before in Sisharp.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Vector2
{
    
   
        public class NewBehaviourScript : MonoBehaviour

        {
            // Start is called before the first frame update
            void Start()
            {

            }

            // Update is called once per frame
            void Update()
            {
                if (Input.GetKey(KeyCode.D))
                    Tranform.Translate(new Vector2(3, 0));//error:is a namespace but i used like a type |and: The name 'Tranform' does not exist in the current context line =(21'21)
            }
        }
    }
namespace Vector2

Why do you have this?

Doing so overrides the use of Vector2 with the namespace rather than the UnityEngine.Vector2. You could type out the full UnityEngine.Vector2 in your code, but why do you have a namespace with the same name as a core Unity struct?

3 Likes

First and foremost: DON’T HIJACK OTHER THREADS

You have a multitude of issues in your code which do not make much sense. If you have issues with your script, create your own thread instead of hijacking other threads.

  • What do you think your line 4 is supposed to do? You’re creating a new namespace called Vector2 which contains a class / type called “NewBehaviourScript”. This is already wrong on so many levels. First of all Vector2 is a type in the UnityEngine namespace which you may even need in the future. Creating a namespace that is equal to an existing class calls for trouble.
  • Give your scripts a meaningful name from the beginning. Keep in mind that the class name and the name of the script file has to match EXACTLY.
  • Next is your line 21. The class is called “Transform”, not “Tranform”. Also you would need the transform property here and not the type. So transform with a lower case “t”.
  • The Translate method of the transform class expects a Vector3. You can pass a Vector2 to the method because Unity implemented implicit type conversion operators between Vector2 and Vector3. Though it would make more sense to use a Vector3 here.

So the actual error you’re referring to in your comment in line 21 should already be solved by point one. Remove the namespace declaration or if you really intended to create a namespace for your class, give it a meaningful dexcriptive name. Though I think you haven’t really understood what a namespace is, so removing it would be the best solution.

1 Like

i deleted namespace .

What name space should I use if I want to move to a character?

please tell me with edit my script

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

public class Example_Script : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKey(KeyCode.D))
            transform.Translate(Vector2.right * Time.deltaTime, Camera.main.transform);
        if (Input.GetKey(KeyCode.A))
            transform.Translate(Vector2.left * Time.deltaTime, Camera.main.transform);
    }
}

Not really sure what the issue is because you didn’t write anything. You’re trying to basically do this, right?

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

public class NewBehaviourScript : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(new Vector3(3f, 0f, 0f) * Time.deltaTime);

            // Alternate example.
            // transform.Translate(Time.deltaTime, 0f, 0f)  
        }
    }
}