- Home /
How to call a function with Vector3[] argument?
How do I call this function?
public void CastArrow (Vector3[] target)
{
GameObject newArrow = Instantiate(arrowGO, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
newArrow.GetComponent<ArrowScript>().target = target[0];
}
I've tried a few ways calling the above function, but I got "Cannot convert Vector3 to int" which totally confused me. I'm trying to call this function by giving it the transform.position of the target(s).
Any ideas? Thanks!
Can we see the code in ArrowScript? It seems like target in that script is an int variable ins$$anonymous$$d of a Vector3 variable.
hmm.. probably my question was misunderstood.
I'm trying to find a way to call CastArrow(new Vector3[gameObject.transform.position]); Whatever that it calls inside shouldn't matter if the error came from the CastArrow(???);
and if it really matters, the ArrowScript's target is Vector3.
Answer by AjayKhara · Sep 23, 2014 at 12:18 PM
This should answer your question.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public GameObject arrowGO;
private Vector3 ArrowScriptTarget;
private Vector3[] targets;
// Use this for initialization
void Start () {
targets = new Vector3[3];
targets [0] = Vector3.zero;
targets [1] = Vector3.one;
targets [2] = transform.position;
CastArrow (targets);
}
public void CastArrow (Vector3[] target)
{
GameObject newArrow = Instantiate(arrowGO, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
ArrowScriptTarget = target[0];
}
}
The error, "Cannot convert Vector3 to int" Is because of this line.
CastArrow(new Vector3[gameObject.transform.position]);
you have to pass a int value which is size of the array to initialize an array.
You can initialize an array, like I did above in the example, or like this,
Vector3[] targets = new [] { new Vector3(0f,0f,0f),
new Vector3(1f,1f,1f) };
So to call the function CastArrow in one line, you can do this.
CastArrow (new [] { new Vector3 (0f, 0f, 0f), new Vector3 (1f, 1f, 1f) });
I hope this answers your question. Cheers!
Answer by dmg0600 · Sep 23, 2014 at 11:46 AM
Transform that function to:
public void CastArrow (Vector3 target)
{
GameObject newArrow = Instantiate(arrowGO, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
newArrow.GetComponent<ArrowScript>().target = target;
}
And call it like this: CastArrow(gameObject.transform.position);
That way you will set the target of your new arrow to the position you passed to the method.
i can't, because there might be more than one targets
sorry, i suck at arrays haha
Then use it with a list and call it with a list:
public void CastArrow (List<Vector3> targets)
{
GameObject newArrow;
for (int i = 0; i < targets.Count; ++i)
{
newArrow = Instantiate(arrowGO, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
newArrow.GetComponent<ArrowScript>().target = target;
}
}
To call you have to build the array first and then call the method:
List<Vector3> listOfTargets = new List<Vector3>();
listOfTargets.Add(firstVector3);
listOfTargets.Add(secondVector3);
...
listOfTargets.Add(whateverVector3);
CastArrow(listOfTargets);
Of course, to do this you have to put this line at the top of your script: using System.Collections.Generic;
Answer by superbsumit · Sep 23, 2014 at 12:48 PM
You are not passing int as an argument when you are declaring new Vector[], it takes int not Vector, this line has a prob :
CastArrow(new Vector3[gameObject.transform.position]);
what you could do is :
Vector3[] array = new [] { new Vector3(0f,0f,0f), new Vector3(1f,1f,1f) };
and pass this when calling function like :
CastArrow(array);
Hope this works :)
Your answer
Follow this Question
Related Questions
private Vector3 function? 2 Answers
Setting position of a transform? 2 Answers
How to check if a vector 3 is in a vector 3 array c#. 1 Answer
How to call a function from another script in C# from array? 1 Answer
C# Convert Vector3[] to Vector2[] 3 Answers