Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AozakiKyuuji · Sep 23, 2014 at 10:55 AM · c#arrayvector3function

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dmg0600 · Sep 23, 2014 at 10:59 AM 0
Share

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.

avatar image AjayKhara · Sep 23, 2014 at 11:04 AM 0
Share

I agree with dmg0600, please show us the ArrowScript.

avatar image AozakiKyuuji · Sep 23, 2014 at 11:34 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AozakiKyuuji · Sep 23, 2014 at 11:47 AM 0
Share

i can't, because there might be more than one targets

sorry, i suck at arrays haha

avatar image dmg0600 · Sep 23, 2014 at 12:14 PM 1
Share

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;

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges