Home > Software design >  My Vector3 variable does not work with the transform.pos of Unity
My Vector3 variable does not work with the transform.pos of Unity

Time:01-22

I am trying to make my cube go to his original position which is 0, 0, 0 but when I run the code an error message tells me

Assets\Code\GameManager.cs(18,41): error CS0103: The name 'originalPosition' does not exist in the current context

this is my code:

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

public class GameManager : MonoBehaviour
{

    public GameObject player;

    // Start is called before the first frame update
    void Start()
    {
        Vector3 originalPosition = new Vector3();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown("r"))
        {
            player.transform.position = originalPosition;
        }
    }
}

Thanks

CodePudding user response:

Well, yes, because there is no implicit conversion between a tuple of ints and a vector.

So, at the very least, you'll need to call one of Vector3's constructor:

Vector3 originalPosition = new Vector3(0,0,0);

But since it's all zeroes, that's just the same as using its default constructor:

Vector3 originalPosition = new Vector3();

...but wait a minute, something's fishy there. You're declaring a local variable with the same name as a field and then doing nothing with that. Perhaps you wanted to initialise your class' field?

originalPosition = new Vector3();

CodePudding user response:

Do you know what is local valuable ?

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

public class GameManager : MonoBehaviour
{

    public GameObject player;
    Vector3 originalPosition;//here

    // Start is called before the first frame update
    void Start()
    {
        originalPosition = new Vector3();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown("r"))
        {
            player.transform.position = originalPosition;
        }
    }
}
  •  Tags:  
  • Related