- Home /
Why when using get; set; in one script i'm getting null in other script that use it ?
This is the first script that clone objects: It does clone the objects but this script and the second one both attached to the same gameobject but for some reason the second script is running first so when i call the public GameObject[] Spaceships get; set; it's null the Spaceships return null. And the script CloneSpaceShips should be running first. Why the editor decide to run first the second script the ObjectsRandomMovements script ?
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class CloneSpaceShips : MonoBehaviour {
public GameObject Spaceship;
private GameObject[] spaceships;
// for tracking properties change
private Vector3 _extents;
private int _spaceshipCount;
private float _spaceshipSize;
/// <summary>
/// How far to place spheres randomly.
/// </summary>
public Vector3 Extents;
/// <summary>
/// How many spheres wanted.
/// </summary>
public int SpaceShipCount;
public float SpaceShipSize;
// Use this for initialization
void Start () {
Clone();
spaceships = GameObject.FindGameObjectsWithTag("SpaceShipToClone");
}
public GameObject[] Spaceships
{
get
{
return spaceships;
}
set
{
spaceships = value;
}
}
private void OnValidate()
{
// prevent wrong values to be entered
Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
SpaceShipCount = Mathf.Max(0, SpaceShipCount);
SpaceShipSize = Mathf.Max(0.0f, SpaceShipSize);
}
private void Reset()
{
Extents = new Vector3(250.0f, 20.0f, 250.0f);
SpaceShipCount = 100;
SpaceShipSize = 20.0f;
}
// Update is called once per frame
void Update () {
}
private void Clone()
{
if (Extents == _extents && SpaceShipCount == _spaceshipCount && Mathf.Approximately(SpaceShipSize, _spaceshipSize))
return;
// cleanup
var ShipsToDestroy = GameObject.FindGameObjectsWithTag("SpaceShip");
foreach (var t in ShipsToDestroy)
{
if (Application.isEditor)
{
DestroyImmediate(t);
}
else
{
Destroy(t);
}
}
var withTag = GameObject.FindWithTag("Terrain");
if (withTag == null)
throw new InvalidOperationException("Terrain not found");
for (var i = 0; i < SpaceShipCount; i++)
{
var o = Instantiate(Spaceship);
o.tag = "SpaceShip";
o.transform.SetParent(base.gameObject.transform);
o.transform.localScale = new Vector3(SpaceShipSize, SpaceShipSize, SpaceShipSize);
// get random position
var x = Random.Range(-Extents.x, Extents.x);
var y = Extents.y; // sphere altitude relative to terrain below
var z = Random.Range(-Extents.z, Extents.z);
// now send a ray down terrain to adjust Y according terrain below
var height = 10000.0f; // should be higher than highest terrain altitude
var origin = new Vector3(x, height, z);
var ray = new Ray(origin, Vector3.down);
RaycastHit hit;
var maxDistance = 20000.0f;
var nameToLayer = LayerMask.NameToLayer("Terrain");
var layerMask = 1 << nameToLayer;
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
{
var distance = hit.distance;
y = height - distance + y; // adjust
}
else
{
Debug.LogWarning("Terrain not hit, using default height !");
}
// place !
o.transform.position = new Vector3(x, y, z);
}
_extents = Extents;
_spaceshipCount = SpaceShipCount;
_spaceshipSize = SpaceShipSize;
}
}
This is the second script but it's running before the CloneSpaceShips so when it's doing this part in the Start function:
CloneSpaceShips ss = new CloneSpaceShips ();
ObjectsToMove = ss.Spaceships;
Then the ObjectsToMove is null since it didn't clone yet the objects in the first script.
What am i missing here ?
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class ObjectsRandomMovements : MonoBehaviour {
private GameObject[] ObjectsToMove;
public float moveSpeed = 50;
private float distanceTravelled;
public bool updateOn = true;
private Vector3 lastPosition;
List<bool> hasRotated = new List<bool>();
List<float> distanceTraveled = new List<float>();
List<Vector3> lastPositions = new List<Vector3>();
List<int> randomNumbers = new List<int>();
private bool test = false;
public float smooth = 1f;
private Vector3 targetAngles;
private void rndNumbers()
{
int Min = 5;
int Max = 500;
System.Random rnd = new System.Random();
randomNumbers = Enumerable.Range(Min, Max).OrderBy(x => rnd.Next()).Take(ObjectsToMove[0].transform.childCount).ToList();
}
// Use this for initialization
void Start () {
CloneSpaceShips ss = new CloneSpaceShips ();
ObjectsToMove = ss.Spaceships;
rndNumbers();
for (int index = 0; index < ObjectsToMove[0].transform.childCount; index++)
{
Transform child = ObjectsToMove[0].transform.GetChild(index);
lastPosition = new Vector3(child.transform.position.x, child.transform.position.y, child.transform.position.z);
lastPositions.Add(lastPosition);
hasRotated.Add(false);
distanceTraveled.Add(0f);
}
}
// Update is called once per frame
void Update () {
MoveObjects();
}
private void MoveObjects()
{
for (int index = 0; index < ObjectsToMove[0].transform.childCount; index++)
{
Transform oneChild = ObjectsToMove[0].transform.GetChild(index);
lastPositions[index] = oneChild.transform.position;
oneChild.transform.position += oneChild.transform.forward * Time.deltaTime * moveSpeed;
}
if (updateOn == true)
{
for (int index = 0; index < ObjectsToMove[0].transform.childCount; index++)
{
Transform child = ObjectsToMove[0].transform.GetChild(index);
distanceTraveled[index] += Vector3.Distance(child.transform.position, lastPositions[index]);
if (distanceTraveled[index] >= randomNumbers[index] && !hasRotated[index])
{
targetAngles = child.transform.eulerAngles + 180f * transform.up;
StartCoroutine(TurnShip(child.transform, child.transform.eulerAngles, targetAngles, smooth));
hasRotated[index] = true;
test = false;
if (test == false)
{
distanceTraveled[index] = 0;
lastPositions[index] = new Vector3(transform.position.x, transform.position.y, transform.position.z);
hasRotated[index] = false;
test = true;
}
}
}
}
}
IEnumerator TurnShip(Transform ship, Vector3 startAngle, Vector3 endAngle, float smooth)
{
float lerpSpeed = 0;
while (lerpSpeed < 1)
{
ship.eulerAngles = Vector3.Lerp(startAngle, endAngle, lerpSpeed);
lerpSpeed += Time.deltaTime * smooth;
yield return null;
}
}
}
CloneSpaceShips ss = new CloneSpaceShips ();
Don't you get the warning You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed. with this?
Right i see the warning. So what other way/s i have to pass the spaceships to the other script without get; set; at all ? And how do i get it in the second script ? I couldn't find how to do it.
What i want is to use the spaceships array from the first script in the second script.
If both scripts are on the same gameobject you can use
CloneSpaceShips ss = GetComponent<CloneSpaceShips>();
Answer by b1gry4n · Oct 18, 2016 at 06:31 AM
If I understand correctly, your issue is that a script you want called second is actually being called first. One way to avoid this is to have the second script not have a void Start, but instead have a function the first script calls when youre ready to run the second. A manually controlled "start". The other way is to change the script execution order: https://docs.unity3d.com/Manual/class-ScriptExecution.html