I get "NullReferenceException: Object not set to an instance of an object" error, while trying to get a value from another script.
//first script
public class Easy : MonoBehaviour
{
public int difficulty=0;
// Start is called before the first frame update
void Start()
{
difficulty = 0;
GetComponent<Button>().onClick.AddListener(diffchange);
}
public void diffchange()
{
difficulty = 1;
SceneManager.LoadScene("5Game");
}
}
//second script
public class AIMovement : MonoBehaviour
{
public GameObject ball;
public float speed;
// Start is called before the first frame update
void Start()
{
if (GetComponent<Easy>().difficulty==1)// here is the problem, trying to get the value from the first script
{
speed = 200;
}
}
// Update is called once per frame
void FixedUpdate()
{
if (ball.transform.position.y > this.transform.position.y)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0, 1) * speed * Time.deltaTime;
}
else
{
if (ball.transform.position.y < this.transform.position.y)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0, -1) * speed * Time.deltaTime;
}
}
}
}
I tried to do it in reverse as well. To change the value in the second script from the first, but it didn't work, i think i got the same error.
//first script
GetComponent<AIMovement>().difficulty=1;
//second script
public int difficulty=0;
...
if(difficulty==1)
...