Search Unity

missing a using directive or an assembly reference?

Discussion in 'Scripting' started by RhinoRunnerGaming29, Jul 2, 2022.

  1. RhinoRunnerGaming29

    RhinoRunnerGaming29

    Joined:
    May 31, 2022
    Posts:
    10
    Keep getting the same error over and over again.

    Assets\scripts\Weapon.cs(183,33): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)

    here is my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. namespace RhinoRunnerGaming
    7. {
    8.     public class Weapon : MonoBehaviourPunCallbacks
    9.     {
    10.         #region Variables
    11.  
    12.         public Gun[] loadout;
    13.         public Transform weaponParent;
    14.         public GameObject bulletHolePrefab;
    15.         public LayerMask canBeShot;
    16.  
    17.         private float currentCooldown;
    18.         private int currentIndex;
    19.         private GameObject currentWeapon;
    20.  
    21.         private bool isReloading;
    22.  
    23.         #endregion
    24.  
    25.         #region Monobehavior Callbacks
    26.  
    27.         private void Start()
    28.         {
    29.             foreach (gun a in loadout) a.Initialize();
    30.             Equip(0);
    31.         }
    32.  
    33.         void Update()
    34.         {
    35.  
    36.             if (photonView.IsMine && Input.GetKeyDown(KeyCode.Alpha1))
    37.             {
    38.                 photonView.RPC("Equip", RpcTarget.All, 0);
    39.             }
    40.  
    41.             if (currentWeapon != null)
    42.             {
    43.                 if (photonView.IsMine)
    44.                 {
    45.                     Aim(Input.GetMouseButton(1));
    46.  
    47.                     if (Input.GetMouseButtonDown(0) && currentCooldown <= 0)
    48.                     {
    49.                         if (loadout[currentIndex].FireBullet())
    50.                         {
    51.                             photonView.RPC("Shoot", RpcTarget.All);
    52.                         }
    53.                         else
    54.                         {
    55.                             loadout[currentIndex].Reload();
    56.                         }
    57.                        
    58.                     }
    59.  
    60.                     if (Input.GetKeyDown(KeyCode.R))
    61.                     {
    62.                         StartCoroutine(Reload(loadout[currentIndex].reload));
    63.                     }
    64.  
    65.                     //CoolDown
    66.                     if (currentCooldown > 0)
    67.                     {
    68.                         currentCooldown -= Time.deltaTime;
    69.                     }
    70.                 }
    71.      
    72.                 //weapon position eleasticity
    73.                 currentWeapon.transform.localPosition = Vector3.Lerp(currentWeapon.transform.localPosition, Vector3.zero, Time.deltaTime * 4f);
    74.  
    75.             }
    76.  
    77.         }
    78.  
    79.         #endregion
    80.  
    81.         #region Private Methods
    82.  
    83.         IEnumerator Reload(float p_wait)
    84.         {
    85.             isReloading = true;
    86.             currentWeapon.SetActive(false);
    87.  
    88.             yield return new WaitForSeconds(p_wait);
    89.  
    90.             loadout[currentIndex].Reload();
    91.             currentWeapon.SetActive(true);
    92.             isReloading = false;
    93.         }
    94.  
    95.         [PunRPC]
    96.         void Equip(int p_ind)
    97.         {
    98.             if (currentWeapon != null)
    99.             {
    100.                 if(isReloading) StopCoroutine("Reload");
    101.                 Destroy(currentWeapon);
    102.             }
    103.  
    104.             currentIndex = p_ind;
    105.  
    106.             GameObject t_newWeapon = Instantiate(loadout[p_ind].prefab, weaponParent.position, weaponParent.rotation, weaponParent) as GameObject;
    107.             t_newWeapon.transform.localPosition = Vector3.zero;
    108.             t_newWeapon.transform.localEulerAngles = Vector3.zero;
    109.             t_newWeapon.GetComponent<sway>().isMine = photonView.IsMine;
    110.  
    111.             currentWeapon = t_newWeapon;
    112.         }
    113.         void Aim(bool p_isAiming)
    114.         {
    115.             Transform t_anchor = currentWeapon.transform.Find("anchor");
    116.             Transform t_state_ads = currentWeapon.transform.Find("states/ADS");
    117.             Transform t_state_hip = currentWeapon.transform.Find("states/hips");
    118.  
    119.             if (p_isAiming)
    120.             {
    121.                 //aim
    122.                 t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_ads.position, Time.deltaTime * loadout[currentIndex].aimspeed);
    123.             }
    124.             else
    125.             {
    126.                 //hip
    127.                 t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_hip.position, Time.deltaTime * loadout[currentIndex].aimspeed);
    128.             }
    129.         }
    130.  
    131.         [PunRPC]
    132.         void Shoot()
    133.         {
    134.             Transform t_spawn = transform.Find("camera/playerCamera");
    135.  
    136.            
    137.  
    138.             //gun fx
    139.             currentWeapon.transform.Rotate(-loadout[currentIndex].recoil, 0, 0);
    140.             currentWeapon.transform.position -= currentWeapon.transform.forward * loadout[currentIndex].kickback;
    141.  
    142.             //setup bloom
    143.             Vector3 t_bloom = t_spawn.position + t_spawn.forward * 1000f;
    144.             t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.up;
    145.             t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.right;
    146.             t_bloom -= t_spawn.position;
    147.             t_bloom.Normalize();
    148.  
    149.             //raycast
    150.             RaycastHit t_hit = new RaycastHit();
    151.             if (Physics.Raycast(t_spawn.position, t_bloom, out t_hit, 1000f, canBeShot))
    152.             {
    153.                 GameObject t_newHole = Instantiate(bulletHolePrefab, t_hit.point + t_hit.normal * 0.001f, Quaternion.identity) as GameObject;
    154.                 t_newHole.transform.LookAt(t_hit.point + t_hit.normal);
    155.                 Destroy(t_newHole, 5f);
    156.  
    157.                 if (photonView.IsMine)
    158.                 {
    159.                     //Shootin other player on network
    160.                     if (t_hit.collider.gameObject.layer == 11)
    161.                     {
    162.                         t_hit.collider.gameObject.GetPhotonView().RPC("TakeDamage", RpcTarget.All, loadout[currentIndex].damage);
    163.                     }
    164.                 }
    165.             }
    166.  
    167.             //cooldown
    168.             currentCooldown = loadout[currentIndex].firerate;
    169.            
    170.         }
    171.  
    172.         [PunRPC]
    173.         private void TakeDamage (int p_damage)
    174.         {
    175.             GetComponent<Player>().TakeDamage(p_damage);
    176.         }
    177.  
    178.  
    179.         #endregion
    180.  
    181.         #region Public Methods
    182.  
    183.         public void RefreshAmmo(Text p_text)
    184.         {
    185.             int t_clip = loadout[currentIndex].GetClip();
    186.             int t_stache = loadout[currentIndex].GetStash();
    187.  
    188.             p_text.text = t_clip.ToString("D2") + " / " + t_stache.ToString("D2");
    189.         }
    190.  
    191.         #endregion
    192.     }
    193.  
    194. }
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Text
    is part of the UI namespace which'll need to import with
    using UnityEngine.UI;


    Otherwise, rename
    Text
    to
    TextAsset
    on line 183 if the
    RefreshAmmo
    parameter should indeed be a
    TextAsset
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    Considering you are asking about this ultra-basic error, it appears you are hammering in someone else's code. Keep this in mind or you are going to waste a lot of your own precious time:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors... see above!
     
    grizzly likes this.
  4. RhinoRunnerGaming29

    RhinoRunnerGaming29

    Joined:
    May 31, 2022
    Posts:
    10
    Thanks

    But...

    I am also getting this error

    Assets\scripts\Weapon.cs(194,6): error CS1513: } expected
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Then you failed to read my post above.

    This forum is not for fixing your typing mistakes. We cannot control your fingers. That's on you.

    Go fix your typing mistakes. See above.
     
    grizzly likes this.