- Home /
Multiplying Vector2 from another script.
Multiplying Vector2 value from another script?
So I am attempting to multiple a single variable from a Vector2 from another script than the one that calls the Vector2. Now I know how I would go about multiplying the entire vector, however I am having trouble just multiplying the x value of the Vector. Here is the script:
function OnMouseDown()
{
object1.GetComponent("TestScript").testingvector = Vector2.x * -1;
}
And I am recieving this error when using it:
BCE0020: An instance of type 'UnityEngine.Vector2' is required to access non static member 'x'.
What would I need to do?
You need to adjust testingvector.x and not Vector2.x. Vector2 is a type. Your testingvector is an actual value comprised of other float values.
Answer by HenryStrattonFW · Nov 06, 2015 at 12:25 PM
As i posted yesterday. The issue here is that Vector2.x is not a thing. if you want to invert the x value of the vector then use the following.
object1.GetComponent("TestScript").testingvector.x *= -1;
Also consider caching a reference to your component instead of doing a GetComponent each update.
Answer by Yiorgos · Nov 06, 2015 at 03:45 PM
Think of Vector2 as a variable type. What you are doing is like doing
x = float + 1.0f;
You should try something like that instead:
object1.GetComponent("TestScript").testingvector.x *= (-1);