Search Unity

Missing a using directive or an assembly reference

Discussion in 'Scripting' started by starguy197, Mar 7, 2014.

Thread Status:
Not open for further replies.
  1. starguy197

    starguy197

    Joined:
    Dec 6, 2013
    Posts:
    7
    Here is the script:
    using UnityEngine;
    using System.Collections;

    [RequireComponent(typeof(PlayerPhysics))]
    public class PlayerController : MonoBehaviour {

    // Player Handling
    public float gravity = 20;
    public float speed = 8;
    public float acceleration = 30;
    public float jumpHeight = 12;

    private float currentSpeed;
    private float targetSpeed;
    private Vector2 amountToMove;

    private PlayerPhysics playerPhysics;


    void Start () {
    playerPhysics = GetComponent<PlayerPhysics>();
    }

    void Update () {
    targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
    currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);

    if (playerPhysics.grounded) {
    amountToMove.y = 0;

    // Jump
    if (Input.GetButtonDown("Jump")) {
    amountToMove.y = jumpHeight;
    }
    }

    amountToMove.x = currentSpeed;
    amountToMove.y -= gravity * Time.deltaTime;
    playerPhysics.Move(amountToMove * Time.deltaTime);
    }

    // Increase n towards target by speed
    private float IncrementTowards(float n, float target, float a) {
    if (n == target) {
    return n;
    }
    else {
    float dir = Mathf.Sign(target - n); // must n be increased or decreased to get closer to target
    n += a * Time.deltaTime * dir;
    return (dir == Mathf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
    }
    }
    }


    I seriously don't get what's wrong. Been trying to figure this out for an hour.
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    What line is the error on?
     
  3. starguy197

    starguy197

    Joined:
    Dec 6, 2013
    Posts:
    7
    17 apparently
    "private PlayerPhysics playerPhysics;"
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I don't think PlayerPhysics is a UnityEngine class. Where does it come from? Is it a script you made yourself?
     
  5. starguy197

    starguy197

    Joined:
    Dec 6, 2013
    Posts:
    7
  6. starguy197

    starguy197

    Joined:
    Dec 6, 2013
    Posts:
    7
    The actual error says this: The type or namespace `PlayerPhysics' could not be found. Are you missing a using directive or an assembly reference?
     
  7. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Do you have the file "PlayerPhysics.cs" somewhere in the Assets folder? Missing that file could be a cause of your error.
     
  8. starguy197

    starguy197

    Joined:
    Dec 6, 2013
    Posts:
    7
    I have it in my assets, according to the video, I made a new folder in assets titled "Scripts", and inserted that.
     
    Nargarcyz likes this.
  9. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Yeah if it's missing I don't know why it's not complaining on:
    Code (csharp):
    1. [RequireComponent(typeof(PlayerPhysics))]
    If the script is in your Assets folder somewhere try this:

    1. Click on the "Assets" menu in Unity
    2. Select "Refresh"

    I'm not sure what else it could be!
     
    kwesley likes this.
  10. starguy197

    starguy197

    Joined:
    Dec 6, 2013
    Posts:
    7
    That gave me 2 more errors
     
  11. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Is the old error still there? Are the new errors different?
     
    SCHNOOKTHEDUCK likes this.
  12. starguy197

    starguy197

    Joined:
    Dec 6, 2013
    Posts:
    7
    Oh, I re-copied the scripts from the WordPad document. That seemed to do the trick, funny thing is, I originally copied the script from WordPad.
     
    SCHNOOKTHEDUCK likes this.
  13. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Awesome! I'm glad something fixed it! Good luck to you.
     
    SCHNOOKTHEDUCK likes this.
  14. kwesley

    kwesley

    Joined:
    Jul 3, 2015
    Posts:
    1
     
  15. dhallihan

    dhallihan

    Joined:
    Dec 2, 2019
    Posts:
    2
    i have this same message but its 'touch' and 'IMEcompositionmode'. this app is the kart tutorial but it sorta happend out of no where, if you can answer asap that would be quite nice
     
  16. Night_shadow17

    Night_shadow17

    Joined:
    Jan 7, 2020
    Posts:
    1
    i have the same problem but with rigidbody and it tells me that
    'Rigidbody' does not contain a definition for 'Addforce' and no accessible extension method 'Addforce' accepting a first argument of type 'Rigidbody' could be found (are you missing a using directive or an assembly reference?)
     
  17. Willsiac62

    Willsiac62

    Joined:
    Jun 27, 2019
    Posts:
    1
    Sometimes Visual Studio doesn't pick up the creation of the new script. Simply close and reopen VS.
     
  18. NYX128

    NYX128

    Joined:
    Aug 2, 2020
    Posts:
    8
    , try using AddForce and not Addforce.
     
  19. NYX128

    NYX128

    Joined:
    Aug 2, 2020
    Posts:
    8
    try AddForce
     
  20. NYX128

    NYX128

    Joined:
    Aug 2, 2020
    Posts:
    8
    c# is cap sensitive so try that
     
  21. nabeelahmed8000

    nabeelahmed8000

    Joined:
    Aug 14, 2020
    Posts:
    1
    I Didn't Get The Option To Insert Rigidbody
    public Ridgidbody rb;
    Getting This Error In This Line : error CS0246: The type or namespace name 'Ridgidbody' could not be found (are you missing a using directive or an assembly reference?)
     
  22. Larck_Drakengold

    Larck_Drakengold

    Joined:
    Aug 15, 2018
    Posts:
    1
    looks like you typod "Rigidbody" in the code portion.
     
  23. kaptnouws

    kaptnouws

    Joined:
    Sep 23, 2019
    Posts:
    1
    For me it was a capital letter mistake
     
  24. phukatatharva02

    phukatatharva02

    Joined:
    Feb 25, 2021
    Posts:
    1
    Assets\move01.cs(5,12): error CS0246: The type or namespace name 'rigidbody' could not be found (are you missing a using directive or an assembly reference?)
    can you please help me with this I could not understand what is the problem
     
  25. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    My problem is that following IheartGameDev's tutorial I get this compiler message for the lines

    Code (Csharp):
    1.  
    2. playerInput.CharacterControls.Jump.started += onJump;
    3. playerInput.CharacterControls.Jump.canceled += onJump;
    4.  
    Does anyone see within this code where Jump should be defined?

    Code (Csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class AnimationMotionController : MonoBehaviour
    8. {
    9.     PlayerInput playerInput;
    10.     CharacterController characterController;
    11.     Animator animator;
    12.  
    13.     int isWalkingHash;
    14.     int isRunningHash;
    15.  
    16.     Vector2 currentMovementInput;
    17.     Vector3 currentMovement;
    18.     Vector3 currentRunMovement;
    19.     bool isMovementPressed;
    20.     bool isRunPressed;
    21.  
    22.     float rotationFactorPerFrame = 1.0f;
    23.     float runMultiplier = 3.0f;
    24.  
    25.     float gravity = -9.8f;
    26.     float groundedGravity = -.05f;
    27.  
    28.     bool isJumpPressed = false;
    29.     float initialJumpVelocity;
    30.     float maxJumpHeight = 1.0f;
    31.     float maxJumpTime = 0.5f;
    32.     bool isJumping = false;
    33.  
    34.     void Awake()
    35.     {
    36.         playerInput = new PlayerInput();
    37.         characterController = GetComponent<CharacterController>();
    38.         animator = GetComponent<Animator>();
    39.  
    40.         isWalkingHash = Animator.StringToHash("isWalking");
    41.         isRunningHash = Animator.StringToHash("isRunning");
    42.  
    43.         playerInput.CharacterControls.Move.started += onMovementInput;
    44.         playerInput.CharacterControls.Move.canceled += onMovementInput;
    45.         playerInput.CharacterControls.Move.performed += onMovementInput;
    46.         playerInput.CharacterControls.Run.started += onRun;
    47.         playerInput.CharacterControls.Run.canceled += onRun;
    48.         playerInput.CharacterControls.Jump.started += onJump;
    49.         playerInput.CharacterControls.Jump.canceled += onJump;
    50.  
    51.         setupJumpVariables();
    52.     }
    53.  
    54.     void setupJumpVariables()
    55.     {
    56.         float timeToApex = maxJumpTime / 2;
    57.         gravity = (-2 * maxJumpHeight) / Mathf.Pow(timeToApex, 2);
    58.         initialJumpVelocity = (2 * maxJumpHeight) / timeToApex;
    59.     }
    60.  
    61.     void handleJump()
    62.     {
    63.         if (!isJumping && characterController.isGrounded && isJumpPressed){
    64.             isJumping = true;
    65.             currentMovement.y = initialJumpVelocity;
    66.             currentRunMovement.y = initialJumpVelocity;
    67.         }
    68.     }
    69.  
    70.     void onJump (InputAction.CallbackContext context)
    71.     {
    72.         isJumpPressed = context.ReadValueAsButton();
    73.         Debug.Log(isJumpPressed);
    74.     }
    75.  
    76.     void onRun(InputAction.CallbackContext context)
    77.     {
    78.         isRunPressed = context.ReadValueAsButton();
    79.     }
    80.  
    81.     void handleRotation()
    82.     {
    83.         Vector3 positionToLookAt;
    84.  
    85.         positionToLookAt.x = currentMovement.x;
    86.         positionToLookAt.y = 0.0f;
    87.         positionToLookAt.z = currentMovement.z;
    88.         Quaternion currentRotation = transform.rotation;
    89.  
    90.      
    91.  
    92.         if (isMovementPressed) {
    93.             Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
    94.             transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame);
    95.         }
    96.     }
    97.  
    98.     void onMovementInput(InputAction.CallbackContext context)
    99.     {
    100.         currentMovementInput = context.ReadValue<Vector2>();
    101.         currentMovement.x = currentMovementInput.x;
    102.         currentMovement.z = currentMovementInput.y;
    103.         currentRunMovement.x = currentMovementInput.x * runMultiplier;
    104.         currentRunMovement.z = currentMovementInput.y * runMultiplier;
    105.         isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
    106.     }
    107.  
    108.     void handleAnimation()
    109.     {
    110.         bool isWalking = animator.GetBool(isWalkingHash);
    111.         bool isRunning = animator.GetBool(isRunningHash);
    112.  
    113.         if(isMovementPressed && !isWalking) {
    114.             animator.SetBool(isWalkingHash, true);
    115.         }
    116.  
    117.         else if (!isMovementPressed && isWalking) {
    118.             animator.SetBool(isWalkingHash, false);
    119.         }
    120.  
    121.         if ((isMovementPressed && isRunPressed) && !isRunning)
    122.         {
    123.             animator.SetBool(isRunningHash, true);
    124.         }
    125.  
    126.         else if ((!isMovementPressed || !isRunPressed) && isRunning) {
    127.             animator.SetBool(isRunningHash, false);
    128.         }
    129.     }
    130.  
    131.     void handleGravity()
    132.     {
    133.         if(characterController.isGrounded){
    134.             currentMovement.y = groundedGravity;
    135.             currentRunMovement.y = groundedGravity;
    136.         } else {
    137.             currentMovement.y += gravity * Time.deltaTime;
    138.             currentRunMovement.y += gravity * Time.deltaTime;
    139.         }
    140.     }
    141.  
    142.     // Update is called once per frame
    143.     void Update()
    144.     {
    145.         handleRotation();
    146.         handleAnimation();
    147.  
    148.         if (isRunPressed){
    149.             characterController.Move(currentRunMovement * Time.deltaTime);
    150.         } else {
    151.              characterController.Move(currentMovement * Time.deltaTime);
    152.         }
    153.         handleGravity();
    154.         handleJump();
    155.     }
    156.  
    157.     void OnEnable()
    158.     {
    159.         playerInput.CharacterControls.Enable();
    160.     }
    161.  
    162.     void OnDisable()
    163.     {
    164.         playerInput.CharacterControls.Disable();
    165.     }
    166. }
    167.  
     
  26. oliver_thiessen25

    oliver_thiessen25

    Joined:
    Feb 9, 2022
    Posts:
    2
    i'm try to fix my game after moveing it from my old computer to a new one
    ```
    using UnityEngine;
    using System.Collections;

    // using UnityEngine.UI;

    public class WaveSpawner : MonoBehaviour
    {

    public Transform enemyPrefab;

    public Transform spawnPoint;

    public float timeBetweenWaves = 5f;
    private float countdown = 2f;

    public Text WaveCountDownText;

    private int waveIndex = 0;

    void Update()
    {
    if (countdown <= 0f)
    {
    StartCoroutine(SpawnWave());
    countdown = timeBetweenWaves;
    }

    countdown -= Time.deltaTime;

    WaveCountDownText.text = Mathf.Round(countdown).ToString();

    }

    IEnumerator SpawnWave()
    {
    waveIndex++;

    for (int i = 0; i < waveIndex; i++)
    {
    SpawnEnemy();
    yield return new WaitForSeconds(0.5f);
    }
    }

    void SpawnEnemy()
    {
    Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
    }

    }
    ```
    the erro messgae is
    Assets/Scenes/Scripts/WaveSpawner.cs(16,9): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)

    Assets/Scenes/Scripts/WaveSpawner.cs(16,9): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)

    Assets/Scenes/Scripts/WaveSpawner.cs(16,9): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)

    Assets/Scenes/Scripts/WaveSpawner.cs(16,9): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)

    Assets/Scenes/Scripts/WaveSpawner.cs(16,9): error CS0246: The type or namespace name 'Text' could not be found (are you missing a using directive or an assembly reference?)
     
  27. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    Seriously? Why do you try to hijack this 8 years old thread? Create your own thread. Also use code tags. The editor here on the forum has buttons up there, you know ^^. Apart from that in your code you have commented out the line
    using UnityEngine.UI;
    and the compiler complains that it can not find some UI related classes, ... mysterious. Those two things can't possibly be related :)
     
  28. UmutGurdal

    UmutGurdal

    Joined:
    Aug 22, 2020
    Posts:
    2
    using Unity.Engine.UI has commented out :)
     
  29. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    That's quite enough necroing. Thread locked.
     
    Bunny83 and Kurt-Dekker like this.
Thread Status:
Not open for further replies.