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 ThadeuMelo · Nov 04, 2016 at 05:13 AM · c#codepageruntime-error

GameObject instanciating only once: Runtime Error

So, guys, I´m realy new to Unity and I´m trying to make a spawn system with random enemies and food for the player in a 2D plane.

I have followed some instrutions and created my Spawn system, below.

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class Respawn : MonoBehaviour {
     public GameObject Player;
     public string AssetName;
     public Transform[] spawnPoints;
     public float spawnTime = 3f;
     public int maxSpawn = 3;
     public int count=0;
 
     private SpriteRenderer spriteRenderer;
     private GameObject SpawnType;
     void Awake()
     {
        
     }
     void Start()
     {
         string assetPath = "Assets/Prefabs/" + AssetName + ".prefab";
         SpawnType = GameObject.Instantiate(AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject))) as GameObject;
         spriteRenderer = GetComponent<SpriteRenderer> ();
         InvokeRepeating ("Spawn",spawnTime,spawnTime);
     }
     //Update is called every frame
     void Update () 
     {
         
     }
 
     void Spawn(){
         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
         Vector3 posicaoAleatoria = Random.insideUnitSphere*100;
         posicaoAleatoria.z = 0;
         this.transform.position = posicaoAleatoria;
         if (maxSpawn >= count)
         {
             if (SpawnType != null)
             {
                 Instantiate(SpawnType,
                 new Vector3(spawnPoints[spawnPointIndex].position.x + posicaoAleatoria.x,
                 spawnPoints[spawnPointIndex].position.y + posicaoAleatoria.y, 0),
                 spawnPoints[spawnPointIndex].rotation);
                 count++;
             }
 
         }
     }
 
 }

I load a prefab from my assets based on the name. I now have two Spawn, one for enemies and other for food. The enemy Spawn is working every time, However, the food Spawn drops only one instance of the food model and I get this error message:

NullReferenceException: Object reference not set to an instance of an object Respawn.Spawn () (at Assets/Scripts/Respawn.cs:41)

With the debug I see the error in the first call, but the object is created in the game goes to next update, after that there is no more food instaciated.

Both Food and Enemy prefabs are 99% the same, right now only the render model is diferent. The null GameObject check I set has no effect, using the debug I get

Any thoughts?

Comment
Add comment · Show 1
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 aditya007 · Nov 04, 2016 at 06:20 AM 0
Share

For instantiating, why don't you simply use Instantiate()? make a public variable prefab, and assign Your prefab in the inspector. Then you can simply call this code

 Instantiate(prefab, position, rotation);


1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Tourist · Nov 04, 2016 at 08:51 AM

One of your transform in your 'spawnPoints' array is null.

  1. Check it inside the inspector of your component.

  2. If none is null, I would recommend to set the variable as a private one and check which classes are modifying it.

Note : Unity serializes private field with [SerializeField] attribute set before the declaration of the variable : [SerializeField] private Transform[] spawnPoints;

Comment
Add comment · Show 1 · 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 ThadeuMelo · Nov 04, 2016 at 02:26 PM 0
Share

Yes, the problem was with spawnPoints references.

It was my old implementation based on a tutorial. I have to set the transform the same way the gameobject.

Thank you.

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

252 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity don't recognize the elements, just the first i put, what can i do?,Unity don't recognize element 1, what can i do? 0 Answers

Need Help With Arduino Code to Unity 0 Answers

c# cods spaceship 0 Answers

How does this code work? 0 Answers

Help with coding crane 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