- Home /
Cannot implicitly convert type 'int' to 'UnityEngine.Vector3'
Hi,
I have recently started to use Unity, and since I'm not a C# expert, I had some problems.
I want to make a car go from behind a building to another building, and loop that so when the car reaches the building, it teleports to the original building to do the same path again and again...
The problem is that I don't know how to express 'When it reaches X position' without getting an error. The error happens at line 13. Also, I noticed I can't put Space.World in Transform.Traslate (it says * isn't a valid operator for Vector3 and Space). Why?
CODE:
using UnityEngine;
using System.Collections;
public class FixedPath : MonoBehaviour {
private Transform transform;
void Start() {
}
// Update is called once per frame
void Update () {
if(transform.forward = -32) {
transform.Translate(Vector3.forward * Time.deltaTime);
}
else {
transform.forward = -27;
}
}
}
I'm sorry for being a noob, i know it's boring to answer stupid questions.
Thanks!
Answer by robertbu · May 07, 2013 at 09:34 PM
transform.forward is a vector point forward in world space, so it has three numbers x,y,z. For your task, you want to check and change the position of the object. Something like:
void Update () {
if(transform.position.z > -27.0f)
transform.position.z = -32.0f;
transform.Translate(Vector3.forward * Time.deltaTime);
}
As for 'Space.World,' I would have to see the exact line of code that generated the error. You likely tried to multiple rather than put in a comma.
Answer by KMKxJOEY1 · May 07, 2013 at 09:33 PM
this:
if(transform.forward = -32)
must be
if(transform.forward == -32)
Answer by Eric5h5 · May 07, 2013 at 09:33 PM
Transform.forward is a Vector3 containing the forward vector for that transform; it can't be an int like "-32". Space.World is an enum and can't be used in math operations like that. See the code examples in the docs for Translate for the exact syntax. Also, you should not have a private variable called "transform". That already exists by default, and means "gameObject.transform".
Your answer
Follow this Question
Related Questions
Method Must have a Return Type (Vector3 x,y,z) 2 Answers
Instantiate object in C# 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Tansform.position assign attempt for 'Enemy(Clone)' is not valid. Input position is {NaN, NaN, NaN} 0 Answers
What is wrong with my zipline script? 2 Answers