Need help in understanding what's wrong with my script (error code - CS0029). How do I fix this?

I want to create respawning platforms for a Unity 3D game, however, I ran into this error code (CS0029)

  • Cannot implicitly convert type ‘UnityEngine.Vector3’ to ‘float’
  • Cannot implicitly convert type ‘float’ to ‘UnityEngine.Vector3’
    I don’t understand what I’m doing wrong as I did exactly what my lecturer had demonstrated (she had no error popping up). How do I fix this?

Here’s the script (it’s incomplete at the moment):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformSpawner : MonoBehaviour
{
public GameObject platform;
float lastpos;
float size;
// Start is called before the first frame update
void Start()
{
lastpos= platform.transform.position;
size = platform.transform.localScale.x;
}
// Update is called once per frame
void Update()
{

}
void spawnX()
{
Vector3 pos = lastpos;
pos.x += size;
Instantiate(platform, pos, Quaternion.identity);
}
void spawnZ()
{
}
}

9619913–1365665–PlatformSpawner.cs (660 Bytes)

Posting your script with code tags makes it a lot more comfortable to read for us:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformSpawner : MonoBehaviour
{
    public GameObject platform;
    float lastpos;
    float size;

    // Start is called before the first frame update
    void Start()
    {
        lastpos= platform.transform.position;
        size = platform.transform.localScale.x;
    }

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

    void spawnX()
    {
        Vector3 pos = lastpos;
        pos.x += size;

        Instantiate(platform, pos, Quaternion.identity);
    }

    void spawnZ()
    {

    }
}

Are you sure you did exactly as your instructor did?

The error message also gives you information about where the error is located. Just look there and compare with your tutorial. You should find your solution that way.

1 Like

Sure there isnt a second copy?

I give up. They’re both the same yet I get errors {for me it’s at lines 14 (lastpos = platform.transform.position) and 26 (Vector3 pos = lastpos) are where my errors are coming from}. Thanks for the reply.

What she has in her script:

You’re trying to assign a Vector3 to a float. They’re two different types that can’t be converted to one another.

I think your lastpos field is meant to be of type Vector3.

2 Likes

Where is that tutorial?

1 Like

When following tutorial videos be aware that single screenshot somewhere in the middle of video isn’t always the final version of code. Maybe the video creator fixes it later. Occasionally tutorials also have link to complete project code, when available compare against that as well. The fact that tutorial author previously demonstrated working result, doesn’t mean that the code they demonstrated so far is already finished and capable of doing that. Sometimes things are shown out of order, to visualize what is being created.

1 Like

Tutorial Link - Sign in to your account

Not a public tutorial.

If it’s a course you are following, why do you ask here?

1 Like

Nevermind, got it figured out eventually. Sometimes, it’s only up to oneself to solve their own issues if no one can.

So, what was the solution?

Actually, it’s always best that you fix the problems you encounter by yourself.