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)

    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];
        if (elements[elementNumber].iPrefabHandleIndex >= elements[elementNumber].tPrefabHandle.Length)
            elements[elementNumber].iPrefabHandleIndex = 0;
        ObjectHandle.up = hitInfoNormal;
        ObjectHandle.position = v3Position;
        ObjectHandle.localEulerAngles = new Vector3(0,0,0);
    else if (elementNumber == (iObstacleCount+iPowerupCount))//currency
        // for generating currency. working perfectly
  • 2
    \$\begingroup\$ Read the error message. It points out where to issue is. \$\endgroup\$ – Varaquilex Jan 20 '16 at 7:05
  • \$\begingroup\$ it displays an error message in "ObjectHandle.gameObject.SetActive(true);' \$\endgroup\$ – Manoj Balaji M Jan 20 '16 at 8:17
  • \$\begingroup\$ After assignment of ObjectHandle, check if it is null or not, by logs or breakpoint. And also add null check after this line \$\endgroup\$ – Hamza Hasan Jan 20 '16 at 9:53
  • \$\begingroup\$ no its not null. i used the same method for currency and obstacles. its working. power ups only not working \$\endgroup\$ – Manoj Balaji M Jan 20 '16 at 9:55

Set the currency value to a very low number, e.g. if you set the value to 10 it will generate a large count.

  • 1
    \$\begingroup\$ I'm not sure what this answer is trying to say. I'd recommend editing it to be more descriptive about the solution, with care to spelling & grammar (eg. is "vow" meant to be "low," and "its ll" meant to be "it will"?) \$\endgroup\$ – DMGregory Mar 2 '16 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.