Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 murkertrer · May 22, 2016 at 08:42 AM · vector3functionreturn value

How to return a Vector3 value?

Hey guys, so I'm trying to figure out how the return of the void works.

I'm trying to get a vector3 from a function;

So I have, Vector3 target in one script, an wish to assign it to the result of this long function (in another script); What I am tryin to get is the return hit.point; at the end of the function

I was trying something like:

                 target = this.GetComponent<AimingSystem> ().AimForHomming ();
 

But it dosen't work :(

Any help much apreciated, thanks for your time

     public void AimForHomming()
     {
         aimingforHomming = true;
 
         y2 += Input.GetAxis ("Mouse Y") * screenSens;
         if (y2 >= sH) {
             y2 = sH;
         }
         if (y2 <= 0) {
             y2 = 0;
         }
 
         x2 += Input.GetAxis ("Mouse X") * screenSens;
         if (x2 >= sW) {
             x2 = sW;
         }
         if (x2 <= 0) {
             x2 = 0;
         }
 
         Vector3 screenPoint = new Vector3 (x2, y2 , 2);
         Vector3 WorldPos = this.GetComponent<Attributes> ().myCam.ScreenToWorldPoint (screenPoint);
         aimObj.SetActive (true);
         aimObj.transform.position = WorldPos;
 
         Ray ray = new Ray (this.GetComponent<Attributes> ().myCam.transform.position,( aimObj.transform.position - this.GetComponent<Attributes> ().myCam.transform.position));
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit, 1000, ~2)) {
             if (hit.transform != this.GetComponent<Attributes> ().myBall) {
                 return hit.point;
             }
         }
     }

Comment
Add comment
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

2 Replies

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

Answer by Munchy2007 · May 22, 2016 at 09:02 AM

Change your function declaration to

public Vector3 AimForHomming() { ...

The use of void means that the function doesn't return anything, if you want to return something your need to replace void with the type that you want to return.

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 murkertrer · May 22, 2016 at 09:33 AM 1
Share

Thank you! this worked!

A side question, is it possible to change the return value of a function? Say that I hit a player,then i want to return a transform, but if I hit another object, just return a Vector3.

avatar image Munchy2007 murkertrer · May 22, 2016 at 11:14 AM 0
Share

You can't vary the return type as such, however you could probably achieve what you want by creating a class that can act as a container for the various return types and some logic to deter$$anonymous$$e how to interpret it and have that as the return value.

Alternatively you could probably also do something using delegates.

I'd suggest you make a post in the community forum for discussion on how you might want to approach this.

avatar image
1

Answer by NoseKills · May 22, 2016 at 09:01 AM

Read the part about return type at the end. C# Methods

Methods can return a value to the caller. If the return type, the type listed before the method name, is not void, the method can return the value by using the return keyword. A statement with the return keyword followed by a value that matches the return type will return that value to the method caller.

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

47 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 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

translate function in Js script to c# 1 Answer

Choosing function with highest return value 2 Answers

Help with function code, passing Vector3 info 1 Answer

Create a Custom Vector3 Object 2 Answers

How would I move an Object after collision? 1 Answer


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