Error Message:Object reference not set to an instance of an object :(

Hi to all members here i am new to Unity and i am creating the tutorial SpaceShooter but of course to learn it better i change some things on my own (e.g i made the hazards explode hitting them with 2 shots increasing the hazard amount at the end of the wave…) and now i want to increase their speed up every time the wave ends
my GameController script is this :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour {
public GameObject hazard;
public Vector3 spawnValues;
public float hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public float levelhardtime;
public float levelmany;
public float Maxseries;
public float speedrep;
public float rep;

int j=1;

void Start()
{
StartCoroutine (Spawnwaves ());
}

IEnumerator Spawnwaves ()
{
//rep = hazard.GetComponent ().levelDif;
yield return new WaitForSeconds (startWait);
while(true)
{
if (j > 1 && j < Maxseries) {
spawnWait = spawnWait - levelhardtime;
hazardCount = hazardCount + levelmany;
hazard.GetComponent().level = true;
rep = rep + 0.5f;
}
else if (j == Maxseries )
{
hazardCount = 0;
}

for (int i = 0; i < hazardCount; i++)

{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);

yield return new WaitForSeconds (spawnWait);

}

yield return new WaitForSeconds (waveWait);
j++;

}

}
}

and My Speed script here:

using UnityEngine;
using System.Collections;
public class Speed : MonoBehaviour
{
public float speed;

public Rigidbody rb;

public bool level = false;
public float levelDif;
void Start ()

{

if (level == false)
{
rb = GetComponent ();
rb.velocity = transform.forward * speed ;
}
if (level == true)
{
GetComponent ().rep = levelDif;
rb = GetComponent ();
rb.velocity = transform.forward * speed * levelDif ;

}
}
}

I get this error and the hazards dont move…i know that the problem is on the : GetComponent ().rep = levelDif;

But i dont know what else to add on it. Thanks

Long post of code is just hard to read without formatting. Please update your post.

To add…Why are you using GetComponent inside the GameController script? You’re trying to get a reference to itself…from itself. You should be able to just set rep directly.

In addition to using code tags, paste the actual error message. It contains a lot of information we can use to narrow it down, most crucially the line numbers of the error - we are just plain not going to read your script line by line to find a NRE. We don’t even do that with our own code.

Sorry guys is my first post here i didnt know how to paste the code properly :frowning:
Here is the code from my GameController script which i want to add level and levelDif from Speed script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour {
public GameObject hazard;
public Vector3 spawnValues;
public float hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public float levelhardtime;
public float levelmany;
public float Maxseries;
public float speedrep;
public float rep;


int j=1;


void Start()
{
StartCoroutine (Spawnwaves ());
}


IEnumerator Spawnwaves ()
{
//rep = hazard.GetComponent<Speed> ().levelDif;
yield return new WaitForSeconds (startWait);
while(true)
{
if (j > 1 && j < Maxseries) {
spawnWait = spawnWait - levelhardtime;
hazardCount = hazardCount + levelmany;
hazard.GetComponent<Speed>().level = true;
rep = rep + 0.5f;
}
else if (j == Maxseries )
{
hazardCount = 0;
}

for (int i = 0; i < hazardCount; i++)

{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);

yield return new WaitForSeconds (spawnWait);

}

yield return new WaitForSeconds (waveWait);
j++;

}

}
}

And here is the Speed script which i have it to add movement to my hazards and laser bolt

using UnityEngine;
using System.Collections;
public class Speed : MonoBehaviour
{
public float speed;


public Rigidbody rb;

public bool level = false;
public float levelDif;
void Start ()

{

if (level == false)
{
rb = GetComponent<Rigidbody> ();
rb.velocity = transform.forward * speed ;
}
if (level == true)
{
GetComponent<GameController> ().rep = levelDif;
rb = GetComponent<Rigidbody> ();
rb.velocity = transform.forward * speed * levelDif ;

}
}
}

I will reference again my problem is what value i should place on GetComponent<GameController> ().rep = levelDif;

So i wont get this error.Thanks
*error code is this and it happens when i start the game:
NullReferenceException: Object reference not set to an instance of an object
Speed.Start () (at Assets/Scripts/Speed.cs:23)

Is your GameController script and your Speed script on the same GameObject?

If not, GetComponent will find nothing, thus you’ll get null (which is what I suspect is happening)

You have to have a reference to the object that has your GameController script on it, or some other way to reference the script.

First of all thanks for your fast replies! I have Speed script attached to my asteroid and on GameController script i have attach asteroid as game object with the name hazard

Well, here’s the thing. hazard.GetComponent() is correct somewhat…but not entirely. The problem is, this targets only one hazard(or a single astroid), so you’re only going to change the speed on one of them if you change the speed using this method.

The other way would involve looping through or updating each speed value as you instantiate the astroid. This works if you plan to have a huge variety.

A third way, if your astroids all always have the same speed would be to make the speed variable static, thus you can change it once using Speed.speed = someValue and all your astroids will use that value. You can then increment it as you want. However, just note that if it’s static, you have to reset it back if the player loses and you plan to start over.

Another way is to have your controller script maintain speed. Then the astroids simply get the speed off your GameController. Thus you maintain a speed variable in one location and the astroids can just request the speed from it on their own.

There are a bunch of ways of doing it. But as you have it now, GetComponent doesn’t work because your Speed script is checking the astroid to try and find a GameController and finding none.

Oh i got that you are right .Unfortunately i cant maintain speed on my GameController because i want to have speed value for my bolt,hazards(astroids) and later for my enemies so it yould be hard to add all these to my main controller.
I found a way doing what im asking with hazard.GetComponent<Speed>().levelDif +=0.5f; on my GameController script after hazard.GetComponent() but i would like to know what should be infront of GetComponent so it could work seperately from my Speed script but its fine i understand it now Thanks a lot:)