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 /
avatar image
0
Question by Emadov_bj · Sep 29, 2017 at 11:14 AM · c#nullreferenceexceptionnull referencenullreferencenull referance exeption

i have written this code to spawn a segments of enemies i don't get errors at all but it didn't work .. what should i do ?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LevelManegar : MonoBehaviour {
 
     public static LevelManegar Instance { set; get;}
 
     private const bool SHOW_COLLIDER = true;
 
     //Level Spawner
 
     private const float DESTANCE_BEFORE_SPAWN = 100f;
     private const int INITIAL_SEGMENT = 10;
     private const int MAX_SEGMENTS_ON_SCREEN = 15;
     private Transform cameraContainer;
     private int amountOfActiveSegments;
     private int continiousSegments;
     private int currentSpawnZ;
     private int currentLevel;
     private int y1, y2, y3;
 
 
     //Lest of Pieces
     public List<Piece> ramps = new List<Piece> ();
     public List<Piece> longblock = new List<Piece> ();
     //public List<Piece> jumps = new List<Piece> ();
     public List<Piece> slides = new List<Piece> ();
     [HideInInspector]
     public List<Piece> Pieces = new List<Piece> (); //All the pieces in the pool
 
     //List of Segments
     public List<Segment> availableSegments = new List<Segment> ();
     public List<Segment> availableTransitions = new List<Segment> ();
     [HideInInspector]
     public List<Segment> segments = new List<Segment> ();
 
     //Game Play 
     private bool isMoving = false;
 
     private void Awake() {
 
         Instance = this;
         cameraContainer = Camera.main.transform;
         currentSpawnZ = 0;
         currentLevel = 0;
     }
 
     private void Start() {
     
         for (int i = 0; i < INITIAL_SEGMENT; i++)
             GenerateSegment ();
     }
 
     private void GenerateSegment() {
     
         SpawnSegment ();
 
         if (Random.Range (0f, 1f) < (continiousSegments * 0.25f)) {
             //Spawn Transition Seg
             continiousSegments = 0;
             SpawnTransition ();
         } 
         else 
         {
             continiousSegments++;
         }
 
     }
 
     private void SpawnSegment ()
     {
         List<Segment> possibleSeg = availableSegments.FindAll (x => x.beginY1 == y1 || x.beginY2 == y2 || x.beginY3 == y3);
         int Id = Random.Range (0, possibleSeg.Count);
 
         Segment s = GetSegment (Id, false);
 
         y1 = s.endY1;
         y2 = s.endY2;
         y3 = s.endY3;
 
         s.transform.SetParent (transform);
         s.transform.localPosition = Vector3.forward * currentSpawnZ;
 
         currentSpawnZ += s.length;
         amountOfActiveSegments++;
         s.Spawn ();
     }
 
     private void SpawnTransition ()
     {
         List<Segment> possibleTransition = availableTransitions.FindAll (x => x.beginY1 == y1 || x.beginY2 == y2 || x.beginY3 == y3);
         int Id = Random.Range (0, possibleTransition.Count);
 
         Segment s = GetSegment (Id, true);
 
         y1 = s.endY1;
         y2 = s.endY2;
         y3 = s.endY3;
 
         s.transform.SetParent (transform);
         s.transform.localPosition = Vector3.forward * currentSpawnZ;
 
         currentSpawnZ += s.length;
         amountOfActiveSegments++;
         s.Spawn ();
     }
 
 
     public Segment GetSegment( int Id, bool transition)
     {
         Segment s = null;
         s = segments.Find (x => x.segId == Id && x.transition == transition && !x.gameObject.activeSelf);
 
         if (s = null) {
             GameObject go = Instantiate ((transition) ? availableTransitions [Id].gameObject : availableSegments [Id].gameObject) as GameObject;
 
             s.segId = Id;
             s.transition = transition;
             segments.Insert (0, s);
         } 
         else 
         {
             segments.Remove (s);
             segments.Insert (0, s);
         }
 
         return s;
     }
         
     public Piece GetPiece(PieceType pt, int visualIndex){
     
         Piece p = Pieces.Find (x => x.Type == pt && x.visualIndex == visualIndex && !x.gameObject.activeSelf); 
 
         if (p == null) 
         {
             GameObject go = null;
             if (pt == PieceType.ramp)
                 go = ramps [visualIndex].gameObject;
             else if (pt == PieceType.longblock)
                 go = longblock [visualIndex].gameObject;
             //else if (pt == PieceType.jump)
                 //go = jumps [visualIndex].gameObject;
             else if (pt == PieceType.slide)
                 go = slides [visualIndex].gameObject;
 
             go = Instantiate (go);
             p = go.GetComponent<Piece> ();
 
             Pieces.Add (p);
         }
 
         return p;
     }
 }
 

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MaxGuernseyIII · Sep 29, 2017 at 08:28 PM

My guess is that it's this line:

 if (s = null) {

Broken down into how that actually executes, that does these things, in order:

  1. Assign null to s

  2. Convert s (which is now null) to a boolean (which will be false)

  3. Test the boolean and choose a branch (which will be the else branch)

Your else branch does not re-initialize s, because (I assume), that's the branch you intended to execute when s was not null.

The fix is pretty simple:

 if (s == null) {

That, instead, does these things:

  1. Test if s is null

  2. If true, goes into the if block

  3. If not, goes into the else block

It's an easy mistake to make and one for which the Unity platform designers share a little bit of blame. This is the wages of an implicit conversion to bool, but in test-driven development there is mitigation of such implicit conversions.

Comment
Add comment · Show 2 · 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 MaxGuernseyIII · Sep 29, 2017 at 08:28 PM 0
Share

One more note: that doesn't mean this is everything that's wrong with the script. It's just a thing that is wrong.

avatar image Emadov_bj · Sep 30, 2017 at 07:21 AM 0
Share

i just checked the error siction in the unity and i have this error

NullReferenceException: Object reference not set to an instance of an object Level$$anonymous$$anegar.SpawnSegment () (at Assets/Scripts/Level$$anonymous$$anegar.cs:78) Level$$anonymous$$anegar.GenerateSegment () (at Assets/Scripts/Level$$anonymous$$anegar.cs:57) Level$$anonymous$$anegar.Start () (at Assets/Scripts/Level$$anonymous$$anegar.cs:52)

avatar image
0

Answer by Eba1337 · Sep 29, 2017 at 01:16 PM

I'm sorry, but have you tried Debug.Log(); to see where it actually reaches on the script? And if your enemies are spawning just not showing? Did you remember to attach the script to an object?

Comment
Add comment · Show 2 · 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 Emadov_bj · Sep 29, 2017 at 07:55 PM 0
Share

i did not try debug.log(); because i have a large script and it will take a hug time to try it .. i did attach to an object but my enemies still not spawning and not showing

avatar image Emadov_bj · Sep 29, 2017 at 08:10 PM 0
Share

i just checked the error siction in the unity and i have this error

NullReferenceException: Object reference not set to an instance of an object Level$$anonymous$$anegar.SpawnSegment () (at Assets/Scripts/Level$$anonymous$$anegar.cs:78) Level$$anonymous$$anegar.GenerateSegment () (at Assets/Scripts/Level$$anonymous$$anegar.cs:57) Level$$anonymous$$anegar.Start () (at Assets/Scripts/Level$$anonymous$$anegar.cs:52)

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

395 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 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

I keep getting a null reference exception, can anyone help? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () 2 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