- Home /
why can´t set value in own function?
Hi, I thought about scripting an value controll function even for slidebars and stuff. javascript:
var cameraDistance : Vector3 = (1,5,10)
/*
x = minimum
y = current value and startvalue
z = maximum
*/
function ValueRangeControll (controllvalue : Vector3) {
if (controllvalue.y < controllvalue.x) {
controllvalue.y = controllvalue.x;
}
//same for y > z
}
function Update () {
cameraDistance += Input.AndSoOn
ValueRangeControll(cameraDistance);
}
but cameraDistance.y goes over 10 and under 1, so it just doesn´t work. (right now I´m using "return cameraDistance.y;" in the function and
cameraDistance.y = ValueRangeControll(cameraDistance);
but I think that is able to be better....)
Why can´t I set the value directly in the function? or how?
(hope my english is ok...)
Answer by whydoidoit · Jul 13, 2012 at 01:25 PM
Your problem is that a Vector3 is a value type and is just copied into the function, your changes are not to the original but to the copy.
Why not just return the whole of controlValue rather than just the y? That would do it.
ok, so I have to return the value if I just use Vector3, float, int and boolean?
Yes and anything else which is a struct (like Quaternions - a few things really). If you see struct in the documentation then you can't just modify its value.
You can if you set your parameter as an out one, like the RayCastHit paramenter in the Physics.Raycast function.
Technically it has to be a ref parameter (not out as the value is also used) and that is in C# and not Unity Script. http://forum.unity3d.com/threads/63060-What-is-quot-ref-quot-in-C-and-its-analog-in-UnityScript
Your answer
Follow this Question
Related Questions
Get Value/String From GUIText 3 Answers
My SendMessage is problematic [SOLVED] 1 Answer
[Improvement] change image based on set value 1 Answer
Unityscript how to change parameters value 1 Answer
What are these things doing in C#? 3 Answers