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!

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.

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!

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 :slight_smile: