- Home /
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;
}
}
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:
Assign null to s
Convert s (which is now null) to a boolean (which will be false)
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:
Test if s is null
If true, goes into the if block
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.
One more note: that doesn't mean this is everything that's wrong with the script. It's just a thing that is wrong.
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)
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?
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
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
Follow this Question
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