Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by sethi-sahil27 · Apr 08, 2016 at 07:46 PM · c#gameobjectreferenceinstancenot

c# Object reference not set to an instance of an object

 using UnityEngine;
 using System.Collections;
 
 public class enemy_movement : MonoBehaviour {
 
         int i=0;
         int num=-1;
         int temp;
         public Transform[] patrolpoints;
         public Transform planetposition;
     public int movespeed;
     private int currentposition;
     public int size;
     GameObject[] enemy;
     GameObject PreFab;
     
     // Use this for initialization
     void Start () {
     
     
     PreFab=(GameObject)Instantiate(Resources.Load("enemy"));
     //currentposition=0;
     }
     
     
     
     
     // Update is called once per frame
     void Update () {
     
     
     
     if(num<2)
     {
     num=num+1;
     temp=random_value();
     
     //this is the error line
 
 
     enemy[num] = (GameObject)GameObject.Instantiate(PreFab, patrolpoints[temp].position, Quaternion.identity);
     
     
     
     }
         for(i=0;i<=num;i++)
         enemy[i].transform.position=Vector3.MoveTowards(enemy[i].transform.position, planetposition.position , movespeed*Time.deltaTime);
     }
     
     
     int random_value()
     {
     int num = Random.Range(0, patrolpoints.Length);
 return num;
     }
 }



i'm new to c#,building my first game on unity.

the error that i get is NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object) enemy_movement.Update () (at Assets/scripts/enemy_movement.cs:50)

this code works for a single enemy ,but not for multiple that are created using an array. any way to correct this or any better way to do this.

thanks.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phxvyper · Apr 08, 2016 at 07:51 PM 0
Share

You don't need to use Resources in order to Load a prefab. You can assign prefabs by clicking and dragging them onto the component's public variable in the editor.

Ensure that the variable is public.

avatar image sethi-sahil27 phxvyper · Apr 08, 2016 at 07:55 PM 0
Share

thanks for your reply, i did what you said, but still the same error.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jessespike · Apr 08, 2016 at 08:20 PM

There's an uninitialized array.

  GameObject[] enemy;

change it to something like:

  GameObject[] enemy = new GameObject[3];
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sethi-sahil27 · Apr 08, 2016 at 08:27 PM 0
Share

hey thanks , it is working now. really appreciate your help.

but what if my game could run for a very long time and then the enemies could be very large in number,how do i handle that, how to declare a dynamic array?

avatar image Jessespike sethi-sahil27 · Apr 08, 2016 at 09:12 PM 0
Share

Use a List.

avatar image sethi-sahil27 Jessespike · Apr 08, 2016 at 10:09 PM 0
Share

thanks again!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Object reference; sometimes works but sometimes doesn't??? 2 Answers

how do you keep high frame rate with 200 or more Gameobjects? 1 Answer

Hi everyone.need help with this. 1 Answer

Need help with GameObject placement 2 Answers

How to Parent a Camera to the Head Bone with No Camera Shake? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges