I have a game where the user drags ‘Trampolines’ into a scene, then fires a cannon, the cannonball bounces off the trampolines and eventually through a hoop. As there can be more than one trampoline I’m instantiating them at the start.
The GameManager decides how many trampolines should be spawned and adds each one to a list. When the cannon is fired it makes a call to the trampoline script to deactivate the rotation arrows around the trampoline. But no matter what I do, this call gives the ‘Object Reference not set to an instance of an object error’.
The GameManager code -
public Trampoline trampoline;
public int trampolineCount;
public List<GameObject> trampolineList = new List<GameObject>();
void Start()
{
for (int i = 0; i < trampolineCount; i++)
{
itemSlot.instantiateTrampoline();
trampolineList.Add(newTrampoline);
}
}
public void CannonFired()
{
foreach (GameObject go in trampolineList)
{
//TrampolineStandard is the name of the script on the trampoline
TrampolineStandard trampScript = go.GetComponent<TrampolineStandard>();
trampScript.HideArrows();
}
}
The Trampoline code
public GameObject arrowLeft;
public GameObject arrowRight;
public void HideArrows()
{
bottomArrow.SetActive(false);
topArrow.SetActive(false);
}
I assume this bug is coming from the object being instantiated, but how the fudge do I fix it