Question unity error cs1061

Statwe

New member
Joined
Oct 1, 2022
Messages
3
Programming Experience
Beginner
hello I was programming and I got this error
Assets\scripts\Player.cs(105,45): error CS1061: 'BulletScript' does not contain a definition for 'Direction' and no accessible extension method 'Direction' accepting a first argument of type 'BulletScript' could be found (are you missing a using directive or an assembly reference?)
Someone tell me what to do here is the code

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public GameObject bulletprefab;
    
    public static Player obj;

    public int lives = 3;

    public bool isGrounded = false;
    public bool isMoving = false;
    public bool isImmune = false;
    

    public float speed = 5f;
    public float jumpForce = 3f;
    public float movHor;

    public float immuneTimeCnt = 0f;
    public float immuneTime = 0.5f;

    public LayerMask groundLayer;
    public float radius = 0.3f;
    public float groundRayDist = 0.5f;

    private Rigidbody2D rb;
    private Animator anim;
    private SpriteRenderer spr;


    void Awake()
    {
        obj = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        spr = GetComponent<SpriteRenderer>();

    }

    





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

        movHor = Input.GetAxisRaw("Horizontal");

        isMoving = (movHor != 0f);

        isGrounded = Physics2D.CircleCast(transform.position, radius, Vector3.down, groundRayDist, groundLayer);

        if (Input.GetKeyDown(KeyCode.Space))
            jump();



        anim.SetBool("isMoving", isMoving);
        anim.SetBool("isGrounded", isGrounded);

        flip(movHor);

        if (movHor < 0.0f) transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
        else if (movHor > 0.0f) transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);

        if (Input.GetKey(KeyCode.F))
        {
            Shoot();
        }

    }


    void FixedUpdate()
    {
        rb.velocity = new Vector2(movHor * speed, rb.velocity.y);
    }

    public void jump()
    {
        if (!isGrounded) return;

        rb.velocity = Vector2.up * jumpForce;
    }

    private void Shoot()
    {
        Vector3 Direction;
        if (transform.localScale.x == 1.0f) Direction = Vector3.right;
        else Direction = Vector3.left;

        GameObject bullet = Instantiate(bulletprefab, transform.position + Direction * 0.1f, Quaternion.identity);
        bullet.GetComponent<BulletScript>().SetDirection(Direction);
    }


    private void flip(float _xValue)
    {
        Vector3 theScale = transform.localScale;

        if (_xValue < 0)
            theScale.x = Mathf.Abs(theScale.x) * -1;
        else
        if (_xValue > 0)
            theScale.x = Mathf.Abs(theScale.x);

        transform.localScale = theScale;
    }

    void OnDestroy()
    {
        obj = null;
    }
}
 
The error you are reporting:
C#:
error CS1061: 'BulletScript' does not contain a definition for 'Direction' and no accessible extension method 'Direction' accepting a first argument of type 'BulletScript' could be found (are you missing a using directive or an assembly reference?)

doesn't seem to match up with this line of code:

C#:
bullet.GetComponent<BulletScript>().SetDirection(Direction);

Are you sure that are compiling the correct Player.cs file? The line of code that you are showing seems to be talking about a method named SetDirection() and that method the argument to that method looks to be a Vector3.
 
The error you are reporting:
C#:
error CS1061: 'BulletScript' does not contain a definition for 'Direction' and no accessible extension method 'Direction' accepting a first argument of type 'BulletScript' could be found (are you missing a using directive or an assembly reference?)

doesn't seem to match up with this line of code:

C#:
bullet.GetComponent<BulletScript>().SetDirection(Direction);

Are you sure that are compiling the correct Player.cs file? The line of code that you are showing seems to be talking about a method named SetDirection() and that method the argument to that method looks to be a Vector3.
A sorry is error CS1061: 'BulletScript' does not contain a definition for 'SetDirection' and no accessible extension method 'SetDirection' accepting a first argument of type 'BulletScript' could be found (are you missing a using directive or an assembly reference?)

you can help me to solve this
 
Without seeing the code for BulletScript the only thing I can guess at is that the class BulletScript doesn't have a accessible method named SetDirection, or you don't have an extension method named SetDirection for the class BulletScript. -- which is exactly what the error is telling you.

If you have a SetDirection() method on your BulletScript class is it declared public?

If you have written a SetDirection() extension method, is it declared public? Does it take a BulletScript as the first parameter?
 
Without seeing the code for BulletScript the only thing I can guess at is that the class BulletScript doesn't have a accessible method named SetDirection, or you don't have an extension method named SetDirection for the class BulletScript. -- which is exactly what the error is telling you.

If you have a SetDirection() method on your BulletScript class is it declared public?

If you have written a SetDirection() extension method, is it declared public? Does it take a BulletScript as the first parameter?
I dont understand you
this is the BulletScript
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletScript : MonoBehaviour
{
    public float Speed;
    private Rigidbody2D rb;
    private Vector2 Direction;

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

    // Update is called once per frame
    private void FixedUpdate()
    {
        rb.velocity = Direction * Speed;
    }

    public void SetDirection(Vector2 direction)
    {
       Direction = direction;   
    }


}
 
Your SetDirection() on line 22 there takes a Vector2. The Direction on line 100 of your post #1 is a Vector3.
 
Back
Top Bottom