Vector2 velocity = new Vector2(0, RightTree.speed)
I have a script to make tress move in background for 2d driving game, but with the line of code above I’m left with “Error CS0120: An object reference is required for the non-static field, method, or property ‘RightTree.speed’ (CS0120) (Assembly-CSharp)”. I would just have it as 4 or something but it’s constantly changing and I know I could copy and paste or rewrite same code over into this script but I want to know of a way too do it like this.
What you first need to do is to set the original Vector 2 as public static variable just like this.
Example:
using UnityEngine;
using System.Collections;
public class originalScript : MonoBehaviour {
public static Vector2 originalVector2; //Here you'll set it as publuc static so you can call it from any other script.
void Start () {
originalVector2 = new Vector2(0,0); //Or whatever your values are.
}
}
Then you can call it on any other script you want. Justt like this.
using UnityEngine;
using System.Collections;
public class staticCall : MonoBehaviour {
Vector2 newVel; //Variable reference in the new script.
void Update () {
newVel = originalScript.originalVector2; //Example of calling the original variable from the original script. First call the original script's name, then the name of the variable followed by a dot.
}
}
Just change the names and values of this example and it should work. Good luck.