Search Unity

Resolved is a namespace but is used like a type

Discussion in 'Scripting' started by christDuNord, May 12, 2021.

  1. christDuNord

    christDuNord

    Joined:
    Oct 24, 2020
    Posts:
    26
    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
     
  2. Code (CSharp):
    1. using MyScriptA.Track;
    2.  
    3. public class MyScriptB: MonoBehaviour
    4. {
    5.     void Shoot()
    6.     {
    7.         var goSwitch = FindObjectOfType<GoSwitch>();
    8.         goSwitch.onActiveLeTruc();
    9.     }
    10. }
    Check out what the code formatter can do on the forum posts... use it!

    (also fixed the namespace too, you're welcome)
     
  3. christDuNord

    christDuNord

    Joined:
    Oct 24, 2020
    Posts:
    26
    Thank you, sorry for the bad code formatter
     
  4. MadKing0p

    MadKing0p

    Joined:
    Mar 2, 2022
    Posts:
    4
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. namespace Vector2
    5. {
    6.    
    7.    
    8.         public class NewBehaviourScript : MonoBehaviour
    9.  
    10.         {
    11.             // Start is called before the first frame update
    12.             void Start()
    13.             {
    14.  
    15.             }
    16.  
    17.             // Update is called once per frame
    18.             void Update()
    19.             {
    20.                 if (Input.GetKey(KeyCode.D))
    21.                     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)
    22.             }
    23.         }
    24.     }
    25.  
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,849
    Code (CSharp):
    1. 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?
     
    MadKing0p, Bunny83 and Deleted User like this.
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    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.
     
    Deleted User likes this.
  7. MadKing0p

    MadKing0p

    Joined:
    Mar 2, 2022
    Posts:
    4
    i deleted namespace .

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

    MadKing0p

    Joined:
    Mar 2, 2022
    Posts:
    4
    please tell me with edit my script
     
  9. MadKing0p

    MadKing0p

    Joined:
    Mar 2, 2022
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Example_Script : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.  
    17.         if (Input.GetKey(KeyCode.D))
    18.             transform.Translate(Vector2.right * Time.deltaTime, Camera.main.transform);
    19.         if (Input.GetKey(KeyCode.A))
    20.             transform.Translate(Vector2.left * Time.deltaTime, Camera.main.transform);
    21.     }
    22. }
    23.  
     
  10. Deleted User

    Deleted User

    Guest

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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.     void Update()
    8.     {
    9.         if (Input.GetKey(KeyCode.D))
    10.         {
    11.             transform.Translate(new Vector3(3f, 0f, 0f) * Time.deltaTime);
    12.  
    13.             // Alternate example.
    14.             // transform.Translate(Time.deltaTime, 0f, 0f)  
    15.         }
    16.     }
    17. }