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 /
This question was closed Feb 19, 2015 at 01:44 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by unin · Feb 19, 2015 at 01:01 AM · c#vector3method

Why is my vector3 value not changing?

I created a method that checks whether the value of a Vector3 has any zeros and if it does changes them to zero. Heres my code:

 using UnityEngine;
 using System.Collections;
 
 public class CheckForZero : MonoBehaviour 
 {
     public Vector3 Vectvalue;
 
     // Use this for initialization
     void Start () 
     {
         Vectvalue = new Vector3(0, 0, 0);
         CheckValue(Vectvalue);
     }
     
 
     void CheckValue(Vector3 vect3)
     {
         if (vect3.x == 0)
             vect3.x = 1;
 
         if (vect3.y == 0)
             vect3.y = 1;
         
         if (vect3.z == 0)
             vect3.z = 1;
     }
 }

The problem is that the value isn't being assigned to the Vector. So my question is why is my vector3 value not changing?

Comment
Add comment · Show 2
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 meat5000 ♦ · Feb 18, 2015 at 11:00 PM 0
Share

Re-assign the vector with the new keyword as you did in start.

avatar image unin · Feb 19, 2015 at 12:03 AM 0
Share

If your suggesting that I assign the value like this:

vec3 = new Vector3(vec3.x,1);

then, I already tried that, also I ran some more test with the script and tried assigning an int and string in the same way and there values stayed the same. Here's some code to show you what I mean:

 using UnityEngine;
 using System.Collections;
 
 public class CheckForZero : $$anonymous$$onoBehaviour 
 {
     public Vector3 Vectvalue;
     public int hamseter = 0;
     public string message = "old message";
 
     // Use this for initialization
     void Start () 
     {
         Vectvalue = transform.position; 
         CheckValue(Vectvalue);
         CheckValue(hamseter);
         StringChange(message);
 
     }
     
 
     void CheckValue(Vector3 vect3)
     {
         if (vect3.x == 0)
             vect3 = new Vector3(1,vect3.y);
 
         if (vect3.y == 0)
             vect3.y = 1;
         
         if (vect3.z == 0)
             vect3.z = 1;
     }
     void CheckValue(int value)
     {
         if (value == 0)
             value = 1;
     }
 
     void StringChange(string _message)
     {
         _message = "new message";
     }
 }

This doesn't seem normal to me so I'm currently in the process of uninstalling unity and rebooting my computer. If I'm missing or not seeing something then please inform me as I am utterly confused right now.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by meat5000 · Feb 19, 2015 at 01:01 AM

After the function the values go nowhere. You are simply not plugging them back in to your vector!

 Vectvalue = new Vector3(vect3.x,vect3.y,vect3.z);

is the magic line you should place at the end of your function.

After all, vect3 was created in the arguments of the function, so it doesn't have scope outside the function.

The other way to do it is to use the return of the function

 Vectvalue = CheckValue(Vectvalue);


 Vector3 CheckValue(Vector3 vect3)
      {

 Vector3 returnVar = new Vector3(vect3.x,vect3.y,vect3.z);
         return returnVar;

I'll let you decide where these fragments go ;)

Comment
Add comment · Show 6 · 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 unin · Feb 19, 2015 at 01:26 AM 0
Share

Wow, can't believe I forgot about variable scope, so if I made the function public then would it be within scope? I was going crazy trying to figure this out. Thank you

avatar image meat5000 ♦ · Feb 19, 2015 at 01:32 AM 0
Share

Your Vectvalue has been declared in the class and so has scope through the whole class, meaning it can be changed from within a function.

Anything declared within a function only has scope within that function whether it is public or private. That simply deter$$anonymous$$es the 'visibility' of that function.

avatar image unin · Feb 19, 2015 at 01:45 AM 0
Share

So it was a poor understanding of how functions work on my part, thank you for your help.

avatar image meat5000 ♦ · Feb 19, 2015 at 01:48 AM 0
Share

You are most welcome.

avatar image Bonfire-Boy · Feb 19, 2015 at 08:15 AM 1
Share

Vector3 is a struct and therefore passed by value not by reference.

$$anonymous$$nowing this fact provides an alternative solution. In the original code, just change the signature of CheckValue to

void CheckValue(ref Vector3 v)

and use the ref keyword when calling it too.

Simple and elegant.

Show more comments

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Get forward direction of face hitted by Raycast. 1 Answer

Roll ball towards Vector3 point (Rigidbody) 0 Answers

Issue's With Positioning 0 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