- Home /
Problem with moving objects in array?
Hi the code below gives the error below and I have no idea why? I'm guessing I'm assigning something wrong but I cant seem to find why on any answers or just my own trouble shooting!
Code:
var coins : GameObject[];
var trigger = false;
var target : GameObject;
var speed : float;
function Start ()
{
coins = GameObject.FindGameObjectsWithTag("Coin");
target = gameObject.transform.position;
speed = 5.0;
}
function Update ()
{
var step = speed * Time.deltaTime;
if (trigger == true)
{
coins = Vector3.MoveTowards(gameObject.transform.position, target.transform.position, step);
}
}
Errors:
Assets/Scripts/Gameplay Functionality Script.js(75,44): BCE0022: Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.GameObject[]'.
Assets/Scripts/Gameplay Functionality Script.js(19,39): BCE0022: Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.GameObject'.
By troubleshooting, did you at least read the error log? It might look like some incomprehensive bulls**t, but, let's just walk you through what it actually means :
Assets/Scripts/Gameplay Functionality Script.js(75,44):
That means the error is located in the file "Gameplay Functionality Script.js" (with a name like that, it should be an error in itself. Try to avoid spaces in your files/folders name and adding "Script" to a script's name is like if every car were named like "Honda Civic Car" or "Toyota Corolla Car"... redundancy is bad).
Furthermore, it indicates the path to the file, which is in the Scripts folder, under the Asset folder.
Next step, those two little numbers :
Gameplay Functionality Script.js(75,44)
This comes to say "There is an error at the 44th character on the 75th line in the file Gameplay Functionality Script.js". This is getting obvious no?
Nest stop, the infamous error.
BCE0022: Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.GameObject[]'
The "BCE0022" is the error code. We don't care much about it because it tells us nothing. BUT, the rest of the sentence... WOW!!! It says that you cannot convert a UnityEngine.Vector3 into a UnityEnigne.GameObject[].
What it means is, you cannot convert a Vector3 to an array of GameObject, which is quite literal isn't it?.
On the 75th line of the Gameplay Functionality Script.js, you are trying to affect a Vecto3 thingy into a GameObject[] thingy and Unity tells you it doesn't understand how to do so, because you can't do it.
Unity does a VERY CLEAR job on pointing out errors, you only have to read them and it will literally (in its figurative sort of meaning) takes you by the hand to bring you to the error.
You can go on and find out what the second error means. (Hint : almost the same s**t).
Answer by pacific00 · Jun 16, 2014 at 04:19 AM
try
if (trigger == true)
{
for(int k = 0; k <coins.length ; k++ )
{
coins[k].transform.position = Vector3.MoveTowards(coins[k].transform.position, target.transform.position, step);
}
}
Answer by robertbu · Jun 16, 2014 at 04:27 AM
'target' is a GameObject. 'gameObject.transform.position' is a Vector3. So on line 9, you are attempting to assign a Vector3 to a GameObject. You cannot do that. My guess is that you want:
target.transform.position = gameObject.transform.position;
On line 18, 'coins' is a GameObject[]. Your Vector3.MoveTowards() returns a Vector3. You are trying to assign a Vector3 to an array of game objects. It won't work. Not sure what you are trying to do here. @pacific00 solutions is as good a guess as any.
Thank you @robertbu that fixed the first error and @pacific00 fixed the second error but the code still doesn't do anything. I put some debug logs in @pacific00 's code and it will print the first one and the first one in the for loop but not the one after the Vector3.$$anonymous$$oveTowards line of code, so for some reason that line isnt getting hit.
function Update ()
{
var step = speed * Time.deltaTime;
if (trigger == true)
{
Debug.Log("In if, trigger is true");
for(var k : int = 0; k < coins.length; k++)
{
Debug.Log("Before changing coins positiions");
coins[k].transform.position = Vector3.$$anonymous$$oveTowards(coins[k].transform.position, target.transform.position, step);
Debug.Log("In for loop, k < coins length");
}
}
}
The only way I can see this happening is if Unity has not imported your script properly. Try resaving and reimporting the script.
And please don't post new questions in the comments.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Array problem -3 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer