Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Necrodoc · Aug 15, 2017 at 12:49 PM · instantiategetcomponentnull reference exception

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);
     }
 
 
 }

Comment
Add comment · Show 1
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 Necrodoc · Aug 15, 2017 at 02:53 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sunderplugs11 · Aug 15, 2017 at 01:40 PM

post the error lol

Comment
Add comment · Show 1 · 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 Necrodoc · Aug 25, 2017 at 11:04 AM 0
Share
 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 ;(

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

125 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

Related Questions

Edit bool on another script and object. 1 Answer

Referencing a GameObject's Components from if Statements (c#) 0 Answers

Unity3d Script does not work anymore 1 Answer

NullReferenceException when referencing to another class 2 Answers

NullReferenceException problems. 0 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