- Home /
The question is answered, right answer was accepted
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?
Re-assign the vector with the new keyword as you did in start.
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.
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 ;)
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
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.
So it was a poor understanding of how functions work on my part, thank you for your help.
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.
Follow this Question
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