Search Unity

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

Discussion in 'Getting Started' started by JimJim02, Feb 2, 2024.

  1. JimJim02

    JimJim02

    Joined:
    Sep 22, 2023
    Posts:
    4
    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()
    {
    }
    }
     

    Attached Files:

  2. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    262
    Posting your script with code tags makes it a lot more comfortable to read for us:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlatformSpawner : MonoBehaviour
    6. {
    7.     public GameObject platform;
    8.     float lastpos;
    9.     float size;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         lastpos= platform.transform.position;
    15.         size = platform.transform.localScale.x;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.    
    22.     }
    23.  
    24.     void spawnX()
    25.     {
    26.         Vector3 pos = lastpos;
    27.         pos.x += size;
    28.  
    29.         Instantiate(platform, pos, Quaternion.identity);
    30.     }
    31.  
    32.     void spawnZ()
    33.     {
    34.  
    35.     }
    36. }
    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.
     
    bugfinders likes this.
  3. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,777
    Sure there isnt a second copy?
     
  4. JimJim02

    JimJim02

    Joined:
    Sep 22, 2023
    Posts:
    4
    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:
    upload_2024-2-2_12-37-16.png
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,899
    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.
     
    bugfinders and JimJim02 like this.
  6. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    262
    Where is that tutorial?
     
    bugfinders likes this.
  7. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    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.
     
    samana1407 likes this.
  8. JimJim02

    JimJim02

    Joined:
    Sep 22, 2023
    Posts:
    4
  9. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,777
  10. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    262
  11. JimJim02

    JimJim02

    Joined:
    Sep 22, 2023
    Posts:
    4
    Nevermind, got it figured out eventually. Sometimes, it's only up to oneself to solve their own issues if no one can.
     
  12. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    262
    So, what was the solution?

    Actually, it's always best that you fix the problems you encounter by yourself.
     
    Last edited: Feb 9, 2024