NullReferenceException?

So I am trying to do something and I assume I am making a very simple mistake. this code doesn’t work:

Rigidbody clone;
CivMoveSpeed = (Random.Range(0,1) - 0.5f) * Random.Range(4,8) * 100f;
clone = Instantiate(Civ, CivSpawn, transform.rotation) as Rigidbody;
clone.velocity = transform.forward * CivMoveSpeed;

but this does:

Rigidbody clone;
CivMoveSpeed = (Random.Range(0,1) - 0.5f) * Random.Range(4,8) * 100f;
clone = Instantiate(Civ, CivSpawn, transform.rotation) as Rigidbody;
//clone.velocity = transform.forward * CivMoveSpeed;

I am not sure where the problem is. I have an almost identical code in another file that works fine. it is saying that the error is coming from commented out line of code. I also tried leaving just the transform and that didn’t work so I tried an entirely new vector which didn’t work either. Any ideas?

Presumably your “Civ” variable is not a Rigidbody, so “clone” is null. (By the way, your code would be more readable if you stick to the standard Unity convention of using lowercase for variables while keeping uppercase for methods and classes.)