NullReferenceException in Two Scripts
I Got NullReferenceException when trying to access variable from another script, also if someone could advise me in improving the code would be great. Any help appreciated.
The script im tryin accessing from: (Accessed variable is set as public float vechiclespeed)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCars : MonoBehaviour
{
public float vechiclespeed;
private bool stopped = false;
private float stoptime;
// Use this for initialization
void Start()
{
stopped = false;
vechiclespeed = 10;
}
void CarMoves()
{
transform.Translate(Vector3.forward * vechiclespeed * Time.deltaTime);
}
void CarStops()
{
stopped = true;
stoptime = 1;
//Debug.Log("Hamuje");
}
// Update is called once per frame
void Update()
{
if (stopped == false)
{
CarMoves();
}
if (stoptime > 0)
{
stoptime = stoptime - 1 * Time.deltaTime; //Debug.Log(stoptime);
}
//if (vechiclespeed > 0) { vechiclespeed = vechiclespeed - 1; }
// transform.Translate(Vector3.forward * vechiclespeed * Time.deltaTime);
if (stoptime == 0)
{
stopped = false;
}
}
void OnTriggerEnter(Collider other)
{
CarStops();
}
/*
void OnTriggerStay(Collider other)
{
CarStops();
//Debug.Log("Stoi");
}
*/
void OnTriggerExit(Collider other)
{
stopped = false;
stoptime = 0;
//Debug.Log("Rusza");
}
}
And the script in which im tryin to change accessed value (anotherscript.vechiclespeed in Void SpawnVechicles with no success):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnThemAll : MonoBehaviour {
public Transform[] SpawnPoints;//liczba punktow tworzenia
public float spawntime = 1f;
public float repeattime = 2f;
private float vechiclesnumber;
public float vechicles_max;
public GameObject[] Vechicles;
private float timepassed;
private string Debug_timepassed;
private string Debug_vechiclesnumber;
private string Debug_vechicles_max;
private Vector3 spawnpointvector;
private MoveCars anotherscript;
// Use this for initialization
void Awake ()
{
anotherscript = GetComponent<MoveCars>();
}
void Start ()
{
InvokeRepeating("SpawnVechicles", spawntime, repeattime);
}
void Debug_Update ()
{
Debug_timepassed = timepassed.ToString();
Debug_vechiclesnumber = vechiclesnumber.ToString();
Debug_vechicles_max = vechicles_max.ToString();
}
// Update is called once per frame
void Update () {
Debug_Update();
timepassed = timepassed + 1 * Time.deltaTime;
}
void CountVechicles ()
{
GameObject[] thingyToFind = GameObject.FindGameObjectsWithTag("Vechicle");
vechiclesnumber = thingyToFind.Length;
}
void SpawnVechicles ()
{
int SpawnIndex = Random.Range(0, SpawnPoints.Length);
int Vechicleindex = Random.Range(0, Vechicles.Length);
CountVechicles();
if (vechiclesnumber < vechicles_max)
{
spawnpointvector = SpawnPoints[SpawnIndex].position;
var hitColliders = Physics.OverlapSphere(spawnpointvector, 0);
if (hitColliders.Length > 0)
{
Debug.Log("SpawnPoint Occupied");
}
else
{
Instantiate(Vechicles[Vechicleindex], SpawnPoints[SpawnIndex].position, SpawnPoints[SpawnIndex].rotation);
anotherscript = GetComponent<MoveCars>();
anotherscript.vechiclespeed = Random.Range(0,10);
Debug.Log("Spawned new vechicle " + vechiclesnumber);
CountVechicles();
// Camera.main.transform.position =
// new Vector3(SpawnPoints[SpawnIndex].position.x+5, SpawnPoints[SpawnIndex].position.y+5, SpawnPoints[SpawnIndex].position.z-5);
//Camera.main.transform.LookAt(SpawnPoints[SpawnIndex].position);
}
}
}
void OnGUI()
{
GUI.Box(new Rect(10, 10, 200, 20), "Time Passed: "+Debug_timepassed);
GUI.Box(new Rect(10, 30, 200, 20), "Vechicles: "+Debug_vechiclesnumber+" / Max: "+Debug_vechicles_max);
}
}
I do keep having this error constantly, clearly im doing something wrong or ... it could be that accessed script is attached only to prefab which is instantiated after few seconds from start?
Answer by sunderplugs11 · Aug 15, 2017 at 01:40 PM
post the error lol
private AccessedScriptName myOtherScript;
void $$anonymous$$ain()
{
var temp = myOtherScript.GetComponent<AccessedScriptName>();
Thise code also gets Null Exception:
NullReferenceException: Object reference not set to an instance of an object
even when i set myOtherScript as private GameObject ins$$anonymous$$d and use GameObject.Find("AccessedScriptName"); in void Start before. Its getting me Null exception error ;(