- Home /
Move one of the objects selected from array ?
Hello Guys, I've been working on a script for 2 days but I coudn't handle. so my script is this;
#pragma strict
//List of ducks
static var duckList : GameObject[];
//Speed of Crocodile
var speedCroco : float;
function Start () {
}
function Update () {
//Move to one of the ducks selected from Array
transform.position = Vector3.MoveTowards (transform.position, duckList[Random.Range(0, duckList.Length)], speedCroco * Time.deltaTime);
}
So Debug Log gaves me this error. "The best overload for the method 'UnityEngine.Vector3.MoveTowards(UnityEngine.Vector3, UnityEngine.Vector3, float)' is not compatible with the argument list '(UnityEngine.Vector3, UnityEngine.GameObject, float)'." I didn't understand what is it meaning ? So Please Help !
Hi Alp,
One thing you should do:
do not use arrays. Use the system "List"
ie, with ...
using System.Collections.Generic;
please look around on here for many examples of it.
It's great to be 14, the perfect time to learn program$$anonymous$$g!
Thank you so much ^_^ There's a short period remains to finish my first project for android platform, thanks for help :)
Fattie. Again, there you go with your blanket statements without proof.
System arrays are completely fine to use. Sometimes you need the other things that List provides, but for day to day simple usage, arrays are fine.
Stop making blanket statements about "Do not do " or "Never do___" without any kind of justification.
Answer by perchik · Sep 03, 2013 at 12:39 PM
Your error tells the answer.
With some spacing , the error says :
float) is not the same as the argument listUnityEngine.Vector3.MoveForwards(UnityEngine.Vector3, UnityEngine.Vector3,
Ufloat) Which means, MoveForwards takes in a Vector3, a Vector3 and a float, but you are passing in a Vector3, a GameObject and float. Your DuckList is an array of GameObjects, so when you get a random gameobject from the array, you can't just pass that gameObject to MoveForwards. Instead you probably want it's position which you access bynityEngine.Vector3.MoveForwards(UnityEngine.Vector3, UnityEngine.GameObject,
.transform.position
like this: transform.position = Vector3.MoveTowards (transform.position, //Vector3 duckList[Random.Range(0, duckList.Length)].transform.position, //Vector3 speedCroco * Time.deltaTime);//float Now, with that said, you might still get an error with the code you've provided since you don't ever assign anything to that array, but I'm hoping that the code you posted was just for the sake of demonstration.
I'm sorry I couldn't understand :( Sorry for my bad english I'm from turkey and 14 years old. Could You explain it basicly ?
I updated my answer to make it more clear, but this is about as simple as I can get it
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
problem with FindWithTag 2 Answers
HELP How to remove gameobject from Built-in Arrays 2 Answers
Array problem? help please! 1 Answer