I am trying to instantiate an obstacles, Currency and Power up in the game. i am using same script to instantiate all. obstacles and currency are working perfectly but power ups is not generating in a game. it displays an error message
NullReferenceException: Object reference not set to an instance of an object ElementsGeneratorss.instantiateElement (Int32 elementNumber, Vector3 v3Position, Single CurrentAngle, Vector3 hitInfoNormal) (at Assets/GameKit/C# Scripts/ElementsGeneratorss.cs:312) ElementsGeneratorss.generateElements (Int32 elementNumber, Single fLocation, Boolean bStartPatch) (at Assets/GameKit/C# Scripts/ElementsGeneratorss.cs:225) ElementsGeneratorss+c__Iterator1.MoveNext () (at Assets/GameKit/C# Scripts/ElementsGeneratorss.cs:115)
i used C# Script
private void instantiateElement(int elementNumber, Vector3 v3Position, float CurrentAngle, Vector3 hitInfoNormal)
{
if (elementNumber < 0)
return;
Transform ObjectHandle;
if (elementNumber < iObstacleCount)//obstacles
{
// for generating obstacles. its working perfectly
}
else if (elementNumber >= iObstacleCount && elementNumber < (iObstacleCount+iPowerupCount))//powerups
{
ObjectHandle = elements[elementNumber].tPrefabHandle[elements[elementNumber].iPrefabHandleIndex];
elements[elementNumber].iPrefabHandleIndex++;
if (elements[elementNumber].iPrefabHandleIndex >= elements[elementNumber].tPrefabHandle.Length)
elements[elementNumber].iPrefabHandleIndex = 0;
ObjectHandle.gameObject.SetActive(true);
ObjectHandle.up = hitInfoNormal;
ObjectHandle.position = v3Position;
ObjectHandle.localEulerAngles = new Vector3(0,0,0);
ObjectHandle.Rotate(0,CurrentAngle,0);
((PowerupScriptss)ObjectHandle.GetComponent(typeof(PowerupScriptss))).initPowerupScript();
}
else if (elementNumber == (iObstacleCount+iPowerupCount))//currency
{
// for generating currency. working perfectly
}
}
ObjectHandle, check if it is null or not, by logs or breakpoint. And also addnullcheck after this line \$\endgroup\$