NullReferenceException after deserializing class from JSON [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 2 years ago.
I'm trying to get a Gesture Recognizer to work on unity 3D. The script let's me choose if I'm recording a gesture or comparing to existing ones if not recording.
The problem is
On the first run, it works perfectly: It lets me draw and record gestures and then, after disabling recording, it compares to existing ones to look for a match on a list (templates.templates).
But then after I close the game and re-open it, it debugs "loading successful" and I can see the list of the templates in the Inspector, but it returns an error of NullRefferenceException for the templates or current gesture. I'm losing my mind here. Can you help me notice what behaves differently from the first run and second (after loading the templates from json file) and figure this thing out?
The script is as follows (I serialized every private thing just because...):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
//******************************************* Gesture Recognizer *********************************************//
//
// Author: Andres De La Fuente Duran
//
// Use: This script can simply be attached to a camera to function.
// It allows for recording of 2D 'gestures', in this case meaning a set of points
// representing a continuous motion of the mouse (or touch input).
// It also allows for testing 'gestures' against a collection of templates created
// with this very same script.
//
// Template gestures are saved in a JSON file and loaded into a list at start.
//
// The way the recognition works is that a list of points is recorded (relative to
// the location of the initial click as (0,0)), scaled to a square resolution of
// choice, reduced by interpolation to a set number of points, and then compared
// to gestures already processed by this script.
//
// This is built for maximum customizability, so you can change the number of points
// allowed when recording, the number of points once reduced, the rate of sampling,
// and the square ratio gestures are scaled to. Recording the gestures and testing
// can be done easily by swithching the 'recording' boolean variable.
//
// Some additional notes: Because the origin of each gesture is the initial
// point, and comparison follows points in order of
// recording, directionality is captured by this
// solution. The gestures do not have to be wildly
// different for the recognition to be reliable.
//
// However, you can turn on 'anomaliesTesting' to
// weight more heavily sudden differences in gestures
// than constant differences to allow for similar
// gestures with small modifications or flares.
//****************************************** Recognizer Class ****************************************************//
//
// Use: Stores all information for the current gesture being recorded, the existing gestures,
// the conditions selected by an editor user, and variables needed to perform recognition.
// This is the central class with most of the functionality in the script.
//
// Fields:
// Editor Controlled............................................................................
//
// recording: boolean to control whether to save a gesture or try to recognize it
//
// anomaliesTesting: boolean to control whether to weight sudden differences during
// comparison more than other differences
//
// pointsPerGesture: the size of the array of points stored for each gesture
//
// templateSaveName: the string name of the gesture to be saved when recording
//
// samplingRate: time interval between samples while recording
//
// maxPointsAllowed: the maximum number of points that will be recorded
//
// standardRatio: the size of one side of the square that points will be scaled to
//
// devTightness: the number of deviations from the average difference between the points
// of two gestures that are allowed before they are weighted more
//
// anomaliesFactor: how much extra to weight the differences that surpass the devTightness
//
// Control Flow................................................................................
//
// gestureStarted: boolean to execute code to start gesture and to avoid starting anew
//
// gestureComplete: boolean to execute recording of gesture until complete
//
// inputReady: boolean to prevent execution of anything until input is lifted
// so as not to start gestures immediately after one is complete
//
// Recording and Recognizing...................................................................
//
// gestureFileName: JSON file to load saved gestures from as templates for recognition
//
// startPoint: the initial point from which to calculate every other point
//
// currentPoint: the last point recorded
//
// currentGesture: the object containing the recorded gesture for current execution
//
// currentPointList: list of points as they are recorded
//
// reducedPoints: array of points for after scaling and mapping of currentPointList
//
// templates: object to store list of template gestures
//
// tempTime: time since last sample
//
// Methods: Documentation is above each significant function
public class GestureRecognizer : MonoBehaviour {
public bool recording = true;
public bool anomaliesTesting = false;
public string templateSaveName;
public int pointsPerGesture = 30;
public float samplingRate = 0.01f;
public bool limitSamples = false;
public int maxPointsAllowed = 100;
public float standardRatio = 100f;
public float devTightness = 1f;
public float anomaliesFactor = 5f;
[SerializeField]private bool gestureStarted;
[SerializeField] private bool gestureComplete;
[SerializeField] private bool inputReady;
[SerializeField] private string gestureFileName = "gestures.json";
[SerializeField] private TwoDPoint startPoint;
[SerializeField] private TwoDPoint currentPoint;
[SerializeField] private DrawnGesture currentGesture;
[SerializeField] private List<TwoDPoint> currentPointList;
[SerializeField] private TwoDPoint[] reducedPoints;
[SerializeField] private GestureTemplates templates;
[SerializeField] private float tempTime = 0f;
private void Awake()
{
}
void Start () {
LoadTemplates();
varInitialization();
}
#region variable initialization and reset
private void varInitialization()
{
currentPoint = new TwoDPoint(0, 0);
startPoint = new TwoDPoint(0, 0);
currentPointList = new List<TwoDPoint>();
currentPointList.Add(new TwoDPoint(0, 0));
reducedPoints = new TwoDPoint[pointsPerGesture];
for (int i = 0; i < pointsPerGesture; i++)
{
reducedPoints[i] = new TwoDPoint(0, 0);
}
gestureStarted = false;
gestureComplete = false;
inputReady = false;
currentGesture = new DrawnGesture("currentGesture", pointsPerGesture);
}
private void varReset()
{
for (int i = 0; i < pointsPerGesture; i++)
{
reducedPoints[i].SetX(0);
reducedPoints[i].SetY(0);
}
currentPointList.Clear();
currentPointList.Add(new TwoDPoint(0,0));
gestureStarted = false;
gestureComplete = false;
}
#endregion
void Update() {
tempTime += Time.deltaTime;
if (Input.GetMouseButton(0))
{
if (inputReady)
{
if (!gestureStarted)
{
gestureStarted = true;
StartGesture();
}
if ((!gestureComplete) && (tempTime > samplingRate))
{
tempTime = 0f;
ContinueGesture();
}
if (gestureComplete)
{
EndGesture();
}
}
} else
{
if (gestureStarted)
{
EndGesture();
}
inputReady = true;
}
}
//******************************************
// Save and Load Gestures
//
// SaveTemplates
// use: writes templates to json file
// LoadTemplates
// use: called on start to read json templates
// object from file if it's there
[SerializeField]
private void SaveTemplates()
{
string filePath = Application.dataPath + "/StreamingAssets/" + gestureFileName;
string saveData = JsonUtility.ToJson(templates);
File.WriteAllText(filePath, saveData);
Debug.Log("Template Saved!");
}
[SerializeField]
private void LoadTemplates()
{
templates = new GestureTemplates();
string filePath = Path.Combine(Application.streamingAssetsPath, gestureFileName);
if (File.Exists(filePath))
{
string data = File.ReadAllText(filePath);
templates = JsonUtility.FromJson<GestureTemplates>(data);
Debug.Log("Templates Loaded!");
}
}
//***************************************
// StartGesture
//
// use: Set up recording of gesture by
// setting the start point and control bool.
// Called when player first clicks.
[SerializeField]
private void StartGesture()
{
Debug.Log("gesture started");
startPoint.SetX(Input.mousePosition.x);
startPoint.SetY(Input.mousePosition.y);
gestureComplete = false;
}
//***************************************
// ContinueGesture
//
// use: Update min and max x and y values for
// the current gesture being recorded
// and add the new point to the list.
// Called while player holds input down.
[SerializeField]
private void ContinueGesture()
{
currentPoint.SetX(Input.mousePosition.x - startPoint.GetX());
currentPoint.SetY(Input.mousePosition.y - startPoint.GetY());
currentPointList.Add(new TwoDPoint(currentPoint.GetX(), currentPoint.GetY()));
if (currentPoint.GetX() > currentGesture.GetMaxX())
{
currentGesture.SetMaxX(currentPoint.GetX());
}
if (currentPoint.GetX() < currentGesture.GetMinX())
{
currentGesture.SetMinX(currentPoint.GetX());
}
if (currentPoint.GetY() > currentGesture.GetMaxY())
{
currentGesture.SetMaxY(currentPoint.GetY());
}
if (currentPoint.GetY() < currentGesture.GetMinY())
{
currentGesture.SetMinY(currentPoint.GetY());
}
if (limitSamples && currentPointList.Count >= maxPointsAllowed)
{
gestureComplete = true;
Debug.Log(message: "Gesture Complete!");
}
}
//***************************************
// EndGesture
//
// use: Resets control bools and other variables
// records gesture to the templates object
// or calls recognition.
// Called when max recording points reached.
[SerializeField]
private void EndGesture()
{
if (inputReady) inputReady = false;
gestureStarted = false;
gestureComplete = true;
Rescale(currentGesture);
MapPoints(currentGesture);
if (recording)
{
currentGesture.SetName(templateSaveName);
templates.templates.Add(new DrawnGesture(currentGesture.GetName(), pointsPerGesture, currentGesture.GetMaxX(), currentGesture.GetMaxY(),
currentGesture.GetMinX(), currentGesture.GetMinY(), currentGesture.GetPoints()));
} else
{
DrawnGesture m = FindMatch(currentGesture, templates);
Debug.Log(m.GetName());
}
varReset();
}
[SerializeField]
private void OnApplicationQuit()
{
SaveTemplates();
}
//***************************************
// Rescale
//
// use: scales recorded list of points to a square field
// of a chosen size by multiplication of the factor
// of the desired size it already is
// Called on every gesture after recording
[SerializeField]
private void Rescale(DrawnGesture gesture)
{
float scale = 1f;
float xrange = gesture.GetMaxX() - gesture.GetMinX();
float yrange = gesture.GetMaxY() - gesture.GetMinY();
if (xrange >= yrange)
{
scale = standardRatio / (gesture.GetMaxX() - gesture.GetMinX());
} else
{
scale = standardRatio / (gesture.GetMaxY() - gesture.GetMinY());
}
if (scale != 1)
{
foreach (TwoDPoint point in currentPointList)
{
point.SetX(point.GetX() * scale);
point.SetY(point.GetY() * scale);
}
}
}
//***************************************
// MapPoints
//
// use: maps the list of recorded points to a desired
// number of points by calculating an even distance
// between such a number of points and interpolating
// when that distance is reached upon traversal of the
// list
// Called after scaling on every gesture
//
// param: gesture: the object to store the new array
[SerializeField]
private void MapPoints(DrawnGesture gesture)
{
reducedPoints[0].SetX(currentPointList[0].GetX());
reducedPoints[0].SetY(currentPointList[0].GetY());
int newIndex = 1;
float totalDistance = TotalDistance();
float coveredDistance = 0;
float thisDistance = 0;
float idealInterval = totalDistance / pointsPerGesture;
for (int i = 0; i < currentPointList.Count - 1; i++)
{
thisDistance = PointDistance(currentPointList[i], currentPointList[i + 1]);
bool passedIdeal = (coveredDistance + thisDistance) >= idealInterval;
if (passedIdeal)
{
TwoDPoint reference = currentPointList[i];
while (passedIdeal && newIndex < reducedPoints.Length)
{
float percentNeeded = (idealInterval - coveredDistance) / thisDistance;
if (percentNeeded > 1f) percentNeeded = 1f;
if (percentNeeded < 0f) percentNeeded = 0f;
float new_x = (((1f - percentNeeded) * reference.GetX()) + (percentNeeded * currentPointList[i + 1].GetX()));
float new_y = (((1f - percentNeeded) * reference.GetY()) + (percentNeeded * currentPointList[i + 1].GetY()));
reducedPoints[newIndex] = new TwoDPoint(new_x, new_y);
reference = reducedPoints[newIndex];
newIndex++;
thisDistance = (coveredDistance + thisDistance) - idealInterval;
coveredDistance = 0;
passedIdeal = (coveredDistance + thisDistance) >= idealInterval;
}
coveredDistance = thisDistance;
} else
{
coveredDistance += thisDistance;
}
gesture.SetPoints(reducedPoints);
}
}
//***************************************
// FindMatch
//
// use: determines template gesture with the minimum
// average distance between points to the
// currently recorded gesture
// Called after finishing a gesture when not
// recording
//
// param: playerGesture: current gesture to be matched
// templates: object containting list of
// gestures to compare against
//
// return: returns gesture object of the minimum
// difference template
[SerializeField]
private DrawnGesture FindMatch(DrawnGesture playerGesture, GestureTemplates templates)
{
float minAvgDifference = float.MaxValue;
DrawnGesture match = new DrawnGesture("no match", pointsPerGesture);
foreach(DrawnGesture template in templates.templates)
{
Debug.Log(template.GetName());
float d = AverageDifference(playerGesture, template);
Debug.Log(d.ToString());
if (d < minAvgDifference)
{
minAvgDifference = d;
match = template;
}
}
return match;
}
//***************************************
// AverageDifference
//
// use: caluclates the average distance between
// the points of two gestures
//
// param: playerGesture: first to be compared
// template: gesture to be compared against
//
// return: returns float value of the average distance
// between points of two parameter gestures
[SerializeField]
private float AverageDifference(DrawnGesture playerGesture, DrawnGesture template)
{
int numPoints = playerGesture.GetNumPoints();
if (numPoints != template.GetNumPoints())
{
Debug.Log("Number of points differs from templates");
return -1f;
}
float totalDifference = 0;
for (int i = 0; i < numPoints; i++)
{
totalDifference += PointDistance(playerGesture.GetPoints()[i], template.GetPoints()[i]);
}
return (totalDifference / numPoints);
}
//***************************************
// AverageDistanceWithAnomalies
//
// use: calculates the average difference between
// the points of two gestures but weighing
// those which deviate significantly by
// multiplying them
// Both the tightness of this and the factor
// of multiplication are customizable
// above
//
// param: playerGesture: first to be compared
// template: gesture to be compared against
//
// return: returns float value of the average distance
// between points of two parameter gestures
// with weights
[SerializeField]
private float AverageDifferenceWithAnomalies(DrawnGesture playerGesture, DrawnGesture template)
{
int numPoints = playerGesture.GetNumPoints();
if (numPoints != template.GetNumPoints())
{
Debug.Log("Number of points differs from templates");
return -1f;
}
float totalDifference = 0;
float[] sampleDifferences = new float[numPoints];
float[] sampleDeviations = new float[numPoints];
float standardDev = 0;
for (int i = 0; i < numPoints; i++)
{
float thisDistance = PointDistance(playerGesture.GetPoints()[i], template.GetPoints()[i]);
sampleDifferences[i] = thisDistance;
totalDifference += thisDistance;
}
float average = totalDifference / numPoints;
for (int i = 0; i < numPoints; i++)
{
sampleDeviations[i] = Math.Abs(sampleDifferences[i] - average);
standardDev += sampleDifferences[i];
}
standardDev = standardDev / numPoints;
for (int i = 0; i < numPoints; i++)
{
if (Math.Abs(sampleDeviations[i]) > devTightness * standardDev)
{
totalDifference -= sampleDifferences[i];
totalDifference += anomaliesFactor * sampleDifferences[i];
}
}
average = totalDifference / numPoints;
return (average);
}
//***************************************
// TotalDistance
//
// use: calculates the total distance covered
// when traversing the current list of recorded
// points in order of recording
// Called when determining ideal intervals
// for mapping onto desired number of points
[SerializeField]
private float TotalDistance()
{
float totalDistance = 0;
for(int i = 0; i < currentPointList.Count - 1; i++)
{
totalDistance += PointDistance(currentPointList[i], currentPointList[i + 1]);
}
Debug.Log("total distance: " + totalDistance);
return totalDistance;
}
//***************************************
// PointDistance
//
// use: calculates the absolute value of the distance
// between two points using pythagorean theorem
[SerializeField]
private float PointDistance(TwoDPoint a, TwoDPoint b)
{
float xDif = a.GetX() - b.GetX();
float yDif = a.GetY() - b.GetY();
return Mathf.Sqrt((xDif * xDif) + (yDif * yDif));
}
}
//******************************************************* Templates ******************************************************//
//
// Use: Groups gestures to be used for comparison to a player's attempts
[Serializable]
public class GestureTemplates
{
public List<DrawnGesture> templates;
public GestureTemplates()
{
templates = new List<DrawnGesture>();
}
}
//******************************************************** Gestures ******************************************************//
//
// Use: Groups all information pertinent to a 'gesture'
// which is essentially a single stroke drawing represented by points
//
// Fields: points: list of points representing the gesture, only populated once a hand drawn gesture is
// reduced by the MapPoints method
//
// min/max: these are the minimum and maximum x and y values of the points (starting point
// is used as the origin)
//
// numPoints: the size of the points array (set to a variable of the GestureRecognizer class to
// keep control there)
//
// name: string that will be returned when matched with a non-recorded gesture
//
// Methods: Initializer(2 parameters): use when creating a new gesture for later use
//
// Initializer(7 parameters): use when copying data from another gesture
//
// Reset: for use in clearing the gesture used for each player gesture attempt
[Serializable]
public class DrawnGesture
{
[SerializeField]private TwoDPoint[] points;
[SerializeField] private string name;
[SerializeField] private float maxX;
[SerializeField] private float minX;
[SerializeField] private float maxY;
[SerializeField] private float minY;
[SerializeField] private int numPoints;
public DrawnGesture(string newName, int pointsPerGesture)
{
numPoints = pointsPerGesture;
points = new TwoDPoint[numPoints];
name = newName;
maxX = 0;
maxY = 0;
}
public DrawnGesture(string newName, int pointsPerGesture, float max_x, float max_y, float min_x, float min_y, TwoDPoint[] newPoints)
{
numPoints = pointsPerGesture;
points = new TwoDPoint[numPoints];
SetPoints(newPoints);
name = newName;
maxX = max_x;
minX = min_x;
maxY = max_y;
minY = min_y;
}
public void Reset()
{
maxX = 0;
minX = 0;
maxY = 0;
minY = 0;
name = "";
Array.Clear(points, 0, numPoints);
}
public TwoDPoint[] GetPoints()
{
return points;
}
public void SetPoints(TwoDPoint[] new_points)
{
for(int i = 0; i < numPoints; i++)
{
points[i] = new TwoDPoint(new_points[i].GetX(), new_points[i].GetY());
}
}
public string GetName()
{
return name;
}
public void SetName(string n)
{
name = n;
}
public float GetMaxX()
{
return maxX;
}
public void SetMaxX(float x)
{
maxX = x;
}
public float GetMaxY()
{
return maxY;
}
public void SetMaxY(float y)
{
maxY = y;
}
public float GetMinY()
{
return minY;
}
public void SetMinY(float y)
{
minY = y;
}
public float GetMinX()
{
return minX;
}
public void SetMinX(float x)
{
minX = x;
}
public int GetNumPoints()
{
return numPoints;
}
public void SetNumPoints(int n)
{
numPoints = n;
}
}
//******************************************************** Points ********************************************************//
//
// Use: This is a class to maintain 2D coordinates
//
// Fields: x: the x coordinate (relative to the first point when recorded)
// y: the y coordinate (also relative to first point)
public class TwoDPoint
{
private float x;
private float y;
public TwoDPoint(float startx, float starty)
{
x = startx;
y = starty;
}
public float GetX()
{
return x;
}
public void SetX(float new_x)
{
x = new_x;
}
public float GetY()
{
return y;
}
public void SetY(float new_y)
{
y = new_y;
}
}
The error is as follows:
NullReferenceException: Object reference not set to an instance of an object
GestureRecognizer.AverageDifference (DrawnGesture playerGesture, DrawnGesture template) (at Assets/Scripts/GestureRecognizer.cs:475)
GestureRecognizer.FindMatch (DrawnGesture playerGesture, GestureTemplates templates) (at Assets/Scripts/GestureRecognizer.cs:437)
GestureRecognizer.EndGesture () (at Assets/Scripts/GestureRecognizer.cs:319)
GestureRecognizer.Update () (at Assets/Scripts/GestureRecognizer.cs:200)
Important:
I did not develop the script, I'm just trying to make it work.
Thanks in advance!

In general: Start Debugging your code, go through it line by line so you see exactly what is null.
I just did that and the Problem is as follows:
The class TwoDPoint is not serializable! Thus the field DrawnGesture.points which is of type TwoDPoint[] is also not serializable.
So when you generate the JSON this field is skipped. There are two places where you could already have noted that:
you could have simply confirmed this in the saved file
{"templates":[{"name":"","maxX":245.0,"minX":-4.0,"maxY":263.0,"minY":0.0,"numPoints":30}]}
&rightarrow; No field named points
you can see that this field points is actually not appearing in the Unity Inspector!
Since the Inspector uses the same Serialization Rules as JsonUtility this should be a hint that something is wrong.
So later for loading the file in LoadTemplates you simply do
templates = JsonUtility.FromJson<GestureTemplates>(data);
but since the field points does not exists in your file and the field is not serializable anyway it is skipped again it keeps the value null for all the loaded templates!
Solution:
Tag your class as [System.Serializable]
[System.Serializable]
public class TwoDPoint
{
...
}
this would almost fix it.
The next problem is that this class contains no serializable fields either! You can change this by again tagging them [SerializeField]:
[System.Serializable]
public class TwoDPoint
{
[SerializeField] private float x;
[SerializeField] private float y;
...
}
Actually the whole getter setter make not much sense in this case ... you could simply make your field public as well
[System.Serializable]
public class TwoDPoint
{
public float X;
public float Y;
public TwoDPoint(float x, float y)
{
X = x;
Y = y;
}
}
and adjust the rest of the code to directly read and write X and Y instead of using get and set methods. public fields of serializable types are automatically serialized.
The change you can already see in the Unity Inspector! As said: If a field doesn't appear here it won't be serialized via JsonUtility neither - and the other way round.
As you can see there now is a field called points in a template!
It is now also present in the JSON
{"templates":[{"points":[{"x":0.0,"y":0.0},{"x":-1.2672364711761475,"y":-4.435328006744385},{"x":-2.534141778945923,"y":-8.870744705200196},{"x":-3.6529128551483156,"y":-13.34582805633545},{"x":-4.771683692932129,"y":-17.820911407470704},{"x":-5.8904547691345219,"y":-22.295995712280275},{"x":-7.009225845336914,"y":-26.771080017089845},{"x":-8.127996444702149,"y":-31.246164321899415},{"x":-9.246767044067383,"y":-35.721248626708987},{"x":-10.365538597106934,"y":-40.19633102416992},{"x":-11.484309196472168,"y":-44.67141342163086},{"x":-12.603079795837403,"y":-49.1464958190918},{"x":-13.577103614807129,"y":-53.655174255371097},{"x":-14.543621063232422,"y":-58.165592193603519},{"x":-15.510139465332032,"y":-62.67601013183594},{"x":-16.438554763793947,"y":-67.18716430664063},{"x":-15.64819049835205,"y":-71.73175811767578},{"x":-14.857826232910157,"y":-76.27635192871094},{"x":-14.067461967468262,"y":-80.8209457397461},{"x":-13.277097702026368,"y":-85.36553955078125},{"x":-12.486733436584473,"y":-89.91014099121094},{"x":-11.696369171142579,"y":-94.4547348022461},{"x":-8.638381004333496,"y":-96.89103698730469},{"x":-4.163297653198242,"y":-98.00981140136719},{"x":0.31178566813468935,"y":-99.12857818603516},{"x":4.815909385681152,"y":-99.94639587402344},{"x":9.422344207763672,"y":-99.70394897460938},{"x":14.028779029846192,"y":-99.46150970458985},{"x":18.63521385192871,"y":-99.21906280517578},{"x":23.241649627685548,"y":-98.97662353515625}],"name":"","maxX":35.0,"minX":-13.0,"maxY":0.0,"minY":-79.0,"numPoints":30}]}
And it is of course also after loading filled with valid values

Related

My position generation method doesn't work

I have a method that is supposed to generate a certain number of Vector3 at a distance not less than specified.
// Generate random point based on plane area
public List<Vector3> GeneratePositions(int numberOfPositions, float minDistanceBetweenPositions)
{
float entireArea = 0f;
List<AreasWeight> areasWeights = new List<AreasWeight>();
List<Vector3> positions = new List<Vector3>();
foreach (GeneratorPlane plane in GeneratorPlanes.GetCollectionAsList())
{
entireArea += plane.GetArea();
}
foreach (GeneratorPlane plane in GeneratorPlanes.GetCollectionAsList())
{
float weight = plane.GetArea() / entireArea;
int numOfPositionsInArea = Mathf.RoundToInt(numberOfPositions * weight);
areasWeights.Add(new(plane, weight, numOfPositionsInArea));
}
foreach (AreasWeight areaWeight in areasWeights)
{
for (int i = 0; i < areaWeight.NumOfPointsInArea; i++)
{
Vector3 generatedPoint = areaWeight.Plane.GetRandomPointOnPlane();
foreach (Vector3 position in positions)
{
int attempts = 1;
while ((position - generatedPoint).magnitude < minDistanceBetweenPositions)
{
generatedPoint = areaWeight.Plane.GetRandomPointOnPlane();
attempts++;
if (attempts > 2000)
{
Debug.Log("Can't generate all positions.");
break;
}
}
}
positions.Add(generatedPoint);
}
}
return positions;
}
Get random point method:
public Vector3 GetRandomPointOnPlane()
{
float xPosition = Random.Range(Mathf.Min(DownPoint.x, DownPointHelper.x), Mathf.Max(DownPoint.x, DownPointHelper.x));
float zPosition = Random.Range(Mathf.Min(DownPoint.z, UpPointHelper.z), Mathf.Max(DownPoint.z, UpPointHelper.z));
return new(xPosition, DownPoint.y + 0.002f, zPosition);
}
But when i Instantiate objects based on these Vector3. Objects still have a distance less than the specified. What am i doing wrong?
I found a solution. The problem was a bad loop structure. When the algorithm confirmed that the distance was too small and generated a new one, it did not check whether the generated position had a gap from the previous positions on the list. It only confirmed that the gap was preserved and the program continued to execute.
I moved the code that makes sure that the distances are saved to the public List<Vector3> GeneratePositions(int numberOfPositions, float minDistanceBetweenPositions) method in the GeneratorPlane class. I also added a private Vector3 PickRandomPos() method to it, just to return the generated position.
Methods in the public class GeneratorPlane:
public Vector3 GetRandomPointOnPlane(List<Vector3> alreadyGeneratedPoints, float minDistnaceBetweenPositions)
{
if (alreadyGeneratedPoints.Count != 0)
{
int attemps = 1;
bool pointFound = false;
Vector3 posToReturn = new();
while (!pointFound)
{
pointFound = true;
posToReturn = PickRandomPos();
foreach (Vector3 position in alreadyGeneratedPoints)
{
if (Vector3.Distance(position, posToReturn) < minDistnaceBetweenPositions)
{
pointFound = false;
attemps++;
if (attemps > 2000)
{
Debug.LogError("Points cannot be generated. Too little available space");
return Vector3.zero;
}
break;
}
}
}
return posToReturn;
}
else
{
Debug.Log("First point generated");
return PickRandomPos();
}
}
private Vector3 PickRandomPos()
{
float xPosition = Random.Range(Mathf.Min(DownPoint.x, DownPointHelper.x), Mathf.Max(DownPoint.x, DownPointHelper.x));
float zPosition = Random.Range(Mathf.Min(DownPoint.z, UpPointHelper.z), Mathf.Max(DownPoint.z, UpPointHelper.z));
return new(xPosition, DownPoint.y + 0.002f, zPosition);
}
Method to generate and return a certain number of items:
public List<Vector3> GeneratePositions(int numberOfPositions, float minDistanceBetweenPositions)
{
float entireArea = 0f;
List<AreasWeight> areasWeights = new();
List<Vector3> positions = new();
foreach (GeneratorPlane plane in PlanesGenerator.GetCollectionAsList())
{
entireArea += plane.GetArea();
}
foreach (GeneratorPlane plane in PlanesGenerator.GetCollectionAsList())
{
float weight = plane.GetArea() / entireArea;
int numOfPositionsInArea = Mathf.RoundToInt(numberOfPositions * weight);
areasWeights.Add(new(plane, weight, numOfPositionsInArea));
}
foreach (AreasWeight areaWeight in areasWeights)
{
for (int i = 0; i < areaWeight.NumOfPointsInArea; i++)
{
Vector3 generatedPoint = areaWeight.Plane.GetRandomPointOnPlane(positions, minDistanceBetweenPositions);
positions.Add(generatedPoint);
}
}
return positions;
}
On the original code if you generate a point 2000 times you actually keep the last generatedPoint, and as you mentioned you don't actually cross check the whole list of positions, only the remaining positions.
Although you have solved your problem and posted a solution, I took the liberty of doing a simple script with the same end, I will share it here in hopes its useful for you or others.
This solution will not fill any area, its only making sure no two objects are at shorter distance than specified.
In my tests, with 50 nPoints only 10/20 points are instantiated before a point takes over 2000 attempts and consequently conclude the search for points. Although this will depend on the ratio between spawnLimits and nPoints.
[SerializeField]
GameObject trunkPrefab;
List<Vector3> positions;
//input variables
int nPoints = 50;
float minDistance = 2.5f;
int spawnLimits = 20;
void Start()
{
positions = new();
for (int i = 0; i < nPoints; i++)
{
Vector3 position = Vector3.zero;
bool newPosition = true;
int attempts = 0;
do
{
//first generation will be automatically added to the list
position = new(Random.Range(-spawnLimits, spawnLimits), .5f, Random.Range(-spawnLimits, spawnLimits));
if (positions.Count < 1)
{
break;
}
//every position will be compared here,
//if any position is too close from then new position
//"newPosition" is set to false and we try again from the start.
for (int p = 0; p < positions.Count; p++)
{
if (Vector3.Distance(position, positions[p]) < minDistance)
{
newPosition = false;
attempts++;
if (attempts > 2000)
{
Debug.Log("Max attempts reached.");
return;
}
break;
}
}
} while (!newPosition);
//adding a random rotation
Vector3 rotation = new(Random.Range(80, 100), Random.Range(0, 179), 0);
Instantiate(trunkPrefab, position, Quaternion.Euler(rotation));
positions.Add(position);
}
}

Unity what's wrong with my instantiating algorithm?

I dont know if I can call this algorithm. But I am working on a game in which player will move in a circular path.
As you can see in the picture player is suppose to orbit the circle. And obstacle shall be instantiated in the circle.I am trying to first create the obstacle in first half(left to the long cube) and then in the second half. But things are getting created in the next half too when code is not supposed to do that. Also, it is showing argument exception error. Please have a look at my code and tell me whether my method is wrong or my formulas are wrong or anything else.
public class ObjectInstantiater : MonoBehaviour {
DataHolder dataholder;
GameObject Obstacle;
LevelData leveldata;
private int currentlevel=0; // default level starts from 0
private List<GameObject> Inactivegameobject = new List<GameObject>(); // this object can be used
private List<GameObject> Activegameobject = new List<GameObject>();
private int totalgameobjects;
private int firsthalfgameobjects, secondhalfgameobjects;
public float outerradius;
public float innerradius;
private bool shallspawnouterradiues = true;
// Use this for initialization
void Awake () {
dataholder = (Object)GameObject.FindObjectOfType<DataHolder>() as DataHolder;
Obstacle = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
leveldata = dataholder.Leveldata[0];
}
void Start()
{
Updateleveldata();
FirstHalf();
}
public int Currentlevel
{
get { return currentlevel; }
set { currentlevel = value;
leveldata = dataholder.Leveldata[currentlevel];//sets the level data
}
}
private void Updateleveldata() // this function gets called after a round
{
totalgameobjects = Random.Range(leveldata.MinimumObstacle, leveldata.MaximumObstacle);
firsthalfgameobjects = Mathf.RoundToInt(totalgameobjects / 2);
secondhalfgameobjects = totalgameobjects - firsthalfgameobjects;
}
private void FirstHalf()
{
Debug.Log(firsthalfgameobjects);
Vector3 pos;
if (Inactivegameobject.Count < firsthalfgameobjects)
{
for (int x = 0; x <= (firsthalfgameobjects - Inactivegameobject.Count); x++)
{
GameObject obs = Instantiate(Obstacle) as GameObject;
obs.SetActive(false);
Inactivegameobject.Add(obs);
}
}
float spawnangledivision = 180 / firsthalfgameobjects;
float spawnangle = 180f;
for(int x = 0; x < firsthalfgameobjects; x++)
{
float proceduralRandomangle = spawnangle;
proceduralRandomangle = Random.Range(proceduralRandomangle , proceduralRandomangle + 2f);
if (shallspawnouterradiues)
{
pos = new Vector3(outerradius * Mathf.Cos(spawnangle), outerradius * Mathf.Sin(spawnangle), 0f);
shallspawnouterradiues = false;
}else
{
pos = new Vector3(innerradius * Mathf.Cos(spawnangle), innerradius * Mathf.Sin(spawnangle), 0f);
shallspawnouterradiues = true;
}
spawnangle += spawnangledivision;
Inactivegameobject[0].SetActive(true); // set it to 0
Inactivegameobject[0].transform.position = pos;
Activegameobject.Add(Inactivegameobject[0]);
Inactivegameobject.RemoveAt(0);
}
}
private void SecondHalf()// No need to check this
{
if (Inactivegameobject.Count < firsthalfgameobjects)
{
GameObject obs = Instantiate(Obstacle) as GameObject;
obs.SetActive(false);
Inactivegameobject.Add(obs);
}
}
}

Can't destroy Transform component of error

I've got some Unity code that creates a grid (2^dimensions in total) of one prefab using a c# script. Upon changing the 'dimensions' value (via the editor for now), I'd like to see all the prefabs get deleted, before Unity updates using OnValidate. Unity does not seem to want to delete the previous set of objects representing the space, as these objects are still accessible in the Unity Hierarchy Pane:
Having trouble destroying objects in unity. the error says:
"Can't destroy Transform component of 'XXX'. If you want to
destroy the game object, please call 'Destroy' on the game object
instead. Destroying the transform component is not allowed."
(refer to function DeletePoints/GeneratePoints. Call Graph: OnValidate --> GeneratePoints(->DeletePoints, ->GeneratePointsHelper)
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections;
public class BinarySpacePointGenerator : MonoBehaviour {
private const int UNITY_DRAW_SPACE_DIMENSIONALITY = 3;
/**
* These values denote spacings for the three dimensional space between binary points.
*/
public float xoff, yoff, zoff;
public float scale;
public Transform pointPrefab;
/**
* The current dimensionality of binary space to be displayed.
*/
public int dimensions;
/*
* The container of points that represent our B space.
*/
private List<Transform> points;
// Use this for initialization
void Start () {
xoff = 1.0f;
yoff = 1.0f;
zoff = 1.0f;
scale = 0.25f;
dimensions = 2;
points = new List<Transform> ();
GeneratePoints ();
}
void OnValidate() {
/* ensure dimensionality */
/* TODO: set up a B^0 space. */
if (dimensions < 1) {
dimensions = 1;
}
if (dimensions >= 13) {
dimensions = 12;
}
/* ensure that our spacing sizes are valid */
if (xoff <= 0.0f) {
xoff = 1.0f;
}
if (yoff <= 0.0f) {
yoff = 1.0f;
}
if (zoff <= 0.0f) {
zoff = 1.0f;
}
if (scale <= 0.0f) {
scale = 0.25f;
}
/* now that we've ensured the dimensionality, we can change the space */
GeneratePoints ();
}
private void DeletePoints() {
for (int i = 0; i < transform.childCount; i++) {
Destroy (transform.GetChild (0));
}
points.RemoveRange(0, points.Count); /* pop off */
}
/**
* Instantiates the points field based on the value of dimensions at call-time.
*/
private void GeneratePoints() {
DeletePoints ();
int[] vectorProto = new int[dimensions];
for (int i = 0; i < dimensions; i++) {
vectorProto [i] = 0;
}
GeneratePointsHelper(vectorProto, dimensions);
}
/**
*
* GeneratePointsHelper
*
* Description: Recursively builds the binary space B^n.
*
* Parameters:
* int[] vector: the proto-type of all higher dimensions for the current trail.
* int n: the number of dimensions left to traverse from this recursion step.
*
* Recursion Termination/Description:
* When n == 0, which means that we have created a unique vector.
*
*/
private void GeneratePointsHelper(int[] vector, int n) {
if (n == 0) {
// use vector to set Sphere object
var point = Instantiate(pointPrefab);
Vector3 pointPosition = new Vector3 ();
pointPosition.x = 0;
pointPosition.y = 0;
pointPosition.z = 0;
for (int i = 0; i < dimensions; i++) {
int d = (i / UNITY_DRAW_SPACE_DIMENSIONALITY);
if ( i % UNITY_DRAW_SPACE_DIMENSIONALITY == 0) {
pointPosition.x += (xoff * vector[i] * Mathf.Pow(2, d));
} else if (i % UNITY_DRAW_SPACE_DIMENSIONALITY == 1) {
pointPosition.y += (yoff * vector[i] * Mathf.Pow(2, d));
} else if (i % UNITY_DRAW_SPACE_DIMENSIONALITY == 2) {
pointPosition.z += (zoff * vector[i] * Mathf.Pow(2, d));
}
}
point.localPosition = pointPosition;
point.localScale = new Vector3 (scale, scale, scale);
point.parent = transform;
points.Add (point);
} else {
vector[dimensions-n] = 0;
GeneratePointsHelper (vector, n - 1);
vector[dimensions-n] = 1;
GeneratePointsHelper (vector, n - 1);
}
}
}
You are currently destroying the GameObjects with Destroy (transform.GetChild (0));.
The problem is that transform.GetChild returns a Transform and you cannot destroy a Transform. With the lastest version Unity, you will get this error:
Can't destroy Transform component of 'GameObject'. If you want to
destroy the game object, please call 'Destroy' on the game object
instead. Destroying the transform component is not allowed.
You need to access the GameObject from the Transform then destroy it. You also need to use i in the GetChild instead of 0 since Destroy is being called in the for loop and that's likely what you are trying to do.
for (int i = 0; i < transform.childCount; i++)
{
Destroy(transform.GetChild(i).gameObject);
}
I'd like to see all the prefabs get deleted, before Unity updates
using OnValidate
Then call DeletePoints() in the first line of the void OnValidate(){} function.
Destroy(Player_RayCasting.LastTower);
changed to
Destroy(Player_RayCasting.LastTower.gameObject);
in my case i was accessing a transform so i had to change it to a game object to delete it

GameObject being set to null after being made

I put together a simple script that creates a sprite-based pointer I can use in UI. It's not complete yet (still needs support for when the player pushes a button), but already running into problems.
I get a NullReferenceException: Object reference not set to an instance of an object
At line 52...
_ptrSpriteRenderer.sprite = newSprite;
...and 72
_ptr.transform.position = new Vector2(((float)_ptrPosX * _ptrPositionXDistance) + _ptrPositionTopLeft.x, ((float)_ptrPosY * _ptrPositionYDistance) + _ptrPositionTopLeft.x);
I am creating _ptr and _ptrSpriteRenderer in the Start, but for some reason at all my other functions these two GameObjects are null, and yet they aren't null during Start.
I'm sure it's something simple that I goofed up on, but I've spent hours comparing this class to other classes where I've created sprites and I can't see the issue.
using UnityEngine;
using System.Collections;
public class Pointer : MonoBehaviour {
private GameObject _ptr;
private Sprite _ptrSprite;
private SpriteRenderer _ptrSpriteRenderer;
private bool _ptrEnabled; // Is the pointer receiving input?
private int _ptrMovePerSecond; // Number of positions to move per second if input held down
private int _ptrXInput;
private int _ptrYInput;
private float _ptrTimeSinceLastInput = 0f;
private float _ptrTimePerInput = 0.25f;
private bool _ptrInputDelay = false;
private Vector2 _ptrPositionTopLeft; //The top left position the pointer can reach in the grid
private Vector2 _ptrPositionBottomRight; //The bottom right position the pointer can reach in the grid
private int _ptrPositionsX; //The number of grid positions the pointer can traverse in X
private int _ptrPositionsY; //The number of grid positions the pointer can traverse in Y
private float _ptrPositionXDistance; //The distance of each X position in the grid
private float _ptrPositionYDistance; //The distance of each Y position in the grid
private int _ptrPosX; //Current X position of pointer in the grid
private int _ptrPosY; //Current Y position of pointer in the grid
// Use this for initialization
void Start () {
_ptr = new GameObject();
_ptrSpriteRenderer = new SpriteRenderer();
_ptr.AddComponent<SpriteRenderer>();
_ptrSpriteRenderer = _ptr.GetComponent<SpriteRenderer>();
_ptrEnabled = true;
}
public void setSprite ( Sprite newSprite )
{
if (newSprite == null)
{
Debug.LogError("No sprite passed to setSprite in Pointer");
}
else
{
_ptrSpriteRenderer.sprite = newSprite;
}
}
public void setPositions (Vector2 positionTopLeft, Vector2 positionBottomRight, int numPositionsX, int numPositionsY)
{
_ptrPositionsX = numPositionsX;
_ptrPositionsY = numPositionsY;
_ptrPositionTopLeft = positionTopLeft;
_ptrPositionBottomRight = positionBottomRight;
_ptrPositionXDistance = Mathf.Abs((positionBottomRight.x - positionTopLeft.x) / numPositionsX);
_ptrPositionYDistance = Mathf.Abs((positionBottomRight.y - positionTopLeft.y) / numPositionsY);
}
public void setPosition (int x, int y)
{
_ptrPosX = x;
_ptrPosY = y;
_ptr.transform.position = new Vector2(((float)_ptrPosX * _ptrPositionXDistance) + _ptrPositionTopLeft.x, ((float)_ptrPosY * _ptrPositionYDistance) + _ptrPositionTopLeft.x);
}
// Update is called once per frame
void Update () {
//Is the pointer enabled?
if (_ptrEnabled)
{
if (_ptrInputDelay)
{
_ptrTimeSinceLastInput += Time.deltaTime;
if (_ptrTimeSinceLastInput >= _ptrTimePerInput)
{
_ptrInputDelay = false;
}
}
if (_ptrInputDelay == false)
{
_ptrXInput = (int)Input.GetAxis("Horizontal");
_ptrYInput = (int)Input.GetAxis("Vertical");
if (_ptrXInput != 0 || _ptrYInput != 0)
{
_ptrPosX += _ptrXInput;
_ptrPosY += _ptrYInput;
Debug.Log("WHEE");
if (_ptrPosX < 0) _ptrPosX = 0;
if (_ptrPosX > _ptrPositionsX) _ptrPosX = _ptrPositionsX;
if (_ptrPosY < 0) _ptrPosY = 0;
if (_ptrPosY > _ptrPositionsY) _ptrPosY = _ptrPositionsY;
_ptr.transform.position = new Vector2(((float)_ptrPosX * _ptrPositionXDistance) + _ptrPositionTopLeft.x, ((float)_ptrPosY * _ptrPositionYDistance) + _ptrPositionTopLeft.x );
_ptrInputDelay = true;
_ptrTimeSinceLastInput = 0f;
}
}
}
}
}
And the place where my Pointer class is being called is done like this:
GameObject newPointer = new GameObject();
newPointer.AddComponent<Pointer>();
Pointer newPointerScript = newPointer.GetComponent<Pointer>();
newPointerScript.setPositions(new Vector2(-1f, -1f), new Vector2(1f, 1f), 3, 3);
newPointerScript.setSprite(newWeapon);
newPointerScript.setPosition(1, 1);
These lines look wrong:
_ptrSpriteRenderer = new SpriteRenderer();
_ptr.AddComponent<SpriteRenderer>();
_ptrSpriteRenderer = _ptr.GetComponent<SpriteRenderer>();
You are creating a SpriteRenderer and then two lines later overwriting the value with what's in _ptr which is, in all probability, null.
Do you really need this line?
Also if you're adding a component shouldn't you be actually passing the component into the Add method?
So turns out everything works fine if I switch Start() with Awake(). That's all that was needed.

objects using their own unique waypoints array

update...
First Class
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Wave
{
public GameObject enemyPrefab;
public float spawnInterval = 2;
public int maxEnemies = 20;
}
public class SpawnEnemy : MonoBehaviour
{
public GameObject[] waypoints;
public GameObject testEnemyPrefab;
public Wave[] waves;
public int timeBetweenWaves = 5;
private GameManagerBehavior gameManager;
private float lastSpawnTime;
private int enemiesSpawned = 0;
// Use this for initialization
void Start()
{
lastSpawnTime = Time.time;
gameManager =
GameObject.Find("GameManager").GetComponent<GameManagerBehavior>();
}
// Update is called once per frame
void Update()
{
// 1 Get the index of the current wave, and check if it’s the last one.
int currentWave = gameManager.Wave;
if (currentWave < waves.Length)
{
// 2 If so, calculate how much time passed since the last enemy spawn and whether it’s time to spawn an enemy. Here you consider two cases.
// If it’s the first enemy in the wave, you check whether timeInterval is bigger than timeBetweenWaves.
// Otherwise, you check whether timeInterval is bigger than this wave’s spawnInterval. In either case, you make sure you haven’t spawned all the enemies for this wave.
float timeInterval = Time.time - lastSpawnTime;
float spawnInterval = waves[currentWave].spawnInterval;
if (((enemiesSpawned == 0 && timeInterval > timeBetweenWaves) ||
timeInterval > spawnInterval) &&
enemiesSpawned < waves[currentWave].maxEnemies)
{
// 3 If necessary, spawn an enemy by instantiating a copy of enemyPrefab. You also increase the enemiesSpawned count.
lastSpawnTime = Time.time;
GameObject newEnemy = (GameObject)
Instantiate(waves[currentWave].enemyPrefab);
newEnemy.GetComponent<MoveEnemy>().waypoints = waypoints;
newEnemy.GetComponent<MoveEnemy>().JiggleWaypoints();
enemiesSpawned++;
}
// 4 You check the number of enemies on screen. If there are none and it was the last enemy in the wave you spawn the next wave.
// You also give the player 10 percent of all gold left at the end of the wave.
if (enemiesSpawned == waves[currentWave].maxEnemies &&
GameObject.FindGameObjectWithTag("Enemy") == null)
{
gameManager.Wave++;
gameManager.Gold = Mathf.RoundToInt(gameManager.Gold * 1.1f);
enemiesSpawned = 0;
lastSpawnTime = Time.time;
}
// 5 Upon beating the last wave this runs the game won animation.
}
else {
gameManager.gameOver = true;
GameObject gameOverText = GameObject.FindGameObjectWithTag("GameWon");
gameOverText.GetComponent<Animator>().SetBool("gameOver", true);
}
}
}
Second Class
using UnityEngine;
using System.Collections;
public class MoveEnemy : MonoBehaviour
{
[System.NonSerialized]
public GameObject[] waypoints;
private int currentWaypoint = 0;
private float lastWaypointSwitchTime;
public float speed = 1.0f;
// Use this for initialization
void Start()
{
lastWaypointSwitchTime = Time.time;
}
// Update is called once per frame
void Update()
{
// 1
Vector3 startPosition = waypoints[currentWaypoint].transform.position;
Vector3 endPosition = waypoints[currentWaypoint + 1].transform.position;
// 2
float pathLength = Vector3.Distance(startPosition, endPosition);
float totalTimeForPath = pathLength / speed;
float currentTimeOnPath = Time.time - lastWaypointSwitchTime;
gameObject.transform.position = Vector3.Lerp(startPosition, endPosition, currentTimeOnPath / totalTimeForPath);
// 3
if (gameObject.transform.position.Equals(endPosition))
{
if (currentWaypoint < waypoints.Length - 2)
{
// 3.a
currentWaypoint++;
lastWaypointSwitchTime = Time.time;
RotateIntoMoveDirection();
}
else {
// 3.b
Destroy(gameObject);
AudioSource audioSource = gameObject.GetComponent<AudioSource>();
AudioSource.PlayClipAtPoint(audioSource.clip, transform.position);
//<< deduct health
GameManagerBehavior gameManager =
GameObject.Find("GameManager").GetComponent<GameManagerBehavior>();
gameManager.Health -= 1;
//>>
}
}
}
public void JiggleWaypoints()
{
for (int i = 1; i < waypoints.Length; i++)
{
waypoints[i].transform.position = new Vector3(waypoints[i].transform.position.x + Random.Range(-3, 3), waypoints[i].transform.position.y + Random.Range(-3, 3), 0);
}
}
private void RotateIntoMoveDirection()
{
//1 It calculates the bug’s current movement direction by subtracting the current waypoint’s position from that of the next waypoint.
Vector3 newStartPosition = waypoints[currentWaypoint].transform.position;
Vector3 newEndPosition = waypoints[currentWaypoint + 1].transform.position;
Vector3 newDirection = (newEndPosition - newStartPosition);
//2 It uses Mathf.Atan2 to determine the angle toward which newDirection points, in radians, assuming zero points to the right.
// Multiplying the result by 180 / Mathf.PI converts the angle to degrees.
float x = newDirection.x;
float y = newDirection.y;
float rotationAngle = Mathf.Atan2(y, x) * 180 / Mathf.PI;
//3 Finally, it retrieves the child named Sprite and rotates it rotationAngle degrees along the z-axis.
// Note that you rotate the child instead of the parent so the health bar — you’ll add it soon — remains horizontal.
GameObject sprite = (GameObject)
gameObject.transform.FindChild("Sprite").gameObject;
sprite.transform.rotation =
Quaternion.AngleAxis(rotationAngle, Vector3.forward);
}
public float distanceToGoal()
{
float distance = 0;
distance += Vector3.Distance(
gameObject.transform.position,
waypoints[currentWaypoint + 1].transform.position);
for (int i = currentWaypoint + 1; i < waypoints.Length - 1; i++)
{
Vector3 startPosition = waypoints[i].transform.position;
Vector3 endPosition = waypoints[i + 1].transform.position;
distance += Vector3.Distance(startPosition, endPosition);
}
return distance;
}
}
Code is working 100% without errors, BUT....
After each spawn all objects get the same waypoint array. This can be seen on the screen as all objects jump to new waypoint in line together each time new object is spawned. I want the object which is already spawn to live life with it's own array of only once created waypoints.
You need to create a new array of waypoints each time you create one from the prefabricated object. You don't show your Instantiate method but I'm guessing that it has a line like this:
this.waypoints = prefab.waypoints;
This will mean that all object you create will share the same list of waypoints (as you've discovered).
What you need is something like this - assuming that the waypoints have X, Y, and Z properties):
this.waypoints = new GameObject[5];
for (int i = 0; i++ ; i < 5)
{
this.waypoints[i].X = prefab.waypoints[i].X;
this.waypoints[i].Y = prefab.waypoints[i].Y;
this.waypoints[i].Z = prefab.waypoints[i].Z;
}
(If you want your points to be a variable length you might want to consider using a list).
This means that each object has a list of unique points even if they start with the same values you can change each independently.
Based on ChrisFs' and Joe Blows' answers, do something like this in your MoveEnemy script:
private Vector3[] myWay;
public void JiggleWaypoints(GameObject[] waypoints)
{
myWay = new Vector3[waypoints.Length];
for(int i = 1; i < waypoints.Length; i++)
{
myWay[i] = new Vector3(waypoints[i].transform.position.x + Random.Range(-3, 4), waypoints[i].transform.position.y + Random.Range(-3, 4), 0);
}
}
myWay replaces the GameObject[].
In your SpawnEnemy script you do this:
GameObject e = (GameObject)Instantiate(enemyPrefab);
e.GetComponent<MoveEnemy>().JiggleWaypoints(waypoints);

Categories