M
M
MrSfinx2020-08-17 09:55:53
Unity
MrSfinx, 2020-08-17 09:55:53

Error in Unity (c#), how to fix?

I did it according to the video lesson, everything works on the video, I double-checked the code, it matches.

Error text:

Assets\Scripts\GameControler.cs(48,21): error CS1061: 'GameObject' does not contain a definition for 'setVector' and no accessible extension method 'setVector' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)

This is the part of the code where the error occurs (it works without it):

public GameObject cubeToCreate, allCubes;

 private void Update()
    {
        if (Input.GetMouseButtonDown(0) || Input.touchCount > 0)
        {
#if !UNITY_EDITOR
            if (Input.GetTouch(0).phase != TouchPhase.Began)
                return;
#endif
            GameObject newCube = Instantiate(
                cubeToCreate,
                cubeToPlase.position,
                Quaternion.identity) as GameObject;

            newCube.transform.SetParent(allCubes.transform);
            newCube.setVector(cubeToPlase.position);
            allCubesPositions.Add(nowCube.getVector());

            SpawnPositions();


        }

setVector is called from here:

struct CubePos
{
    public int x, y, z;

    public CubePos (int x, int y, int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public Vector3 getVector()
    {
        return new Vector3(x, y, z);

    }
    public void setVector(Vector3 pos)
    {
        x = Convert.ToInt32(pos.x);
        y = Convert.ToInt32(pos.y);
        z = Convert.ToInt32(pos.z);

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2020-08-17
@MrSfinx

Video lesson - bullshit. It can't work like that here. You have newCube - GameObject, not CubePos. And since CubePos is a structure (and not a monobehic for example) - it's even hard to guess where the error is and what the author wanted to say with this code.
So ask a question in the comments below the video tutorial

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question