• 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
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 vec$$anonymous$$clespeed)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveCars : MonoBehaviour
 {
 
 
     public float vec$$anonymous$$clespeed;
     private bool stopped = false;
     private float stoptime;
 
 
     // Use t$$anonymous$$s for initialization
     void Start()
     {
         stopped = false;
         vec$$anonymous$$clespeed = 10;
     }
 
     void CarMoves()
     {
         transform.Translate(Vector3.forward * vec$$anonymous$$clespeed * 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 (vec$$anonymous$$clespeed > 0) { vec$$anonymous$$clespeed = vec$$anonymous$$clespeed - 1; }
         // transform.Translate(Vector3.forward * vec$$anonymous$$clespeed * 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 w$$anonymous$$ch im tryin to change accessed value (anotherscript.vec$$anonymous$$clespeed in Void SpawnVec$$anonymous$$cles 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 vec$$anonymous$$clesnumber;
     public float vec$$anonymous$$cles_max;
     public GameObject[] Vec$$anonymous$$cles;
 
     private float timepassed;
 
     private string Debug_timepassed;
     private string Debug_vec$$anonymous$$clesnumber;
     private string Debug_vec$$anonymous$$cles_max;
     private Vector3 spawnpointvector;
 
     private MoveCars anotherscript;
 
         // Use t$$anonymous$$s for initialization
 
     void Awake ()
     {
         anotherscript = GetComponent<MoveCars>();
     }
     void Start ()
     {
         InvokeRepeating("SpawnVec$$anonymous$$cles", spawntime, repeattime);
     }
 
     void Debug_Update ()
     {
         Debug_timepassed = timepassed.ToString();
         Debug_vec$$anonymous$$clesnumber = vec$$anonymous$$clesnumber.ToString();
         Debug_vec$$anonymous$$cles_max = vec$$anonymous$$cles_max.ToString();
     }
     
     // Update is called once per frame
     void Update () {
         
         Debug_Update();
         timepassed = timepassed + 1 * Time.deltaTime;
 
     }
 
     void CountVec$$anonymous$$cles ()
     {
         GameObject[] t$$anonymous$$ngyToFind = GameObject.FindGameObjectsWithTag("Vec$$anonymous$$cle");
         vec$$anonymous$$clesnumber = t$$anonymous$$ngyToFind.Length;
     }
 
     void SpawnVec$$anonymous$$cles ()
     {             
            
         int SpawnIndex = Random.Range(0, SpawnPoints.Length);
         int Vec$$anonymous$$cleindex = Random.Range(0, Vec$$anonymous$$cles.Length);
 
         CountVec$$anonymous$$cles();
 
         if (vec$$anonymous$$clesnumber < vec$$anonymous$$cles_max)
         {
             spawnpointvector = SpawnPoints[SpawnIndex].position;
             var $$anonymous$$tColliders = Physics.OverlapSphere(spawnpointvector, 0);
             if ($$anonymous$$tColliders.Length > 0)
             {
                 Debug.Log("SpawnPoint Occupied");               
             }
             else
             {
                 Instantiate(Vec$$anonymous$$cles[Vec$$anonymous$$cleindex], SpawnPoints[SpawnIndex].position, SpawnPoints[SpawnIndex].rotation);
 
     
                 anotherscript = GetComponent<MoveCars>();
                 anotherscript.vec$$anonymous$$clespeed = Random.Range(0,10);             
 
 
                 Debug.Log("Spawned new vec$$anonymous$$cle " + vec$$anonymous$$clesnumber);
                 CountVec$$anonymous$$cles();
 
                 // 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), "Vec$$anonymous$$cles: "+Debug_vec$$anonymous$$clesnumber+" / Max: "+Debug_vec$$anonymous$$cles_max);
     }
 
 
 }

Comment

People who like this

0 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

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

post the error lol

Comment

People who like this

0 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 Main()
 {
 
 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 instead 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Unity3d Script does not work anymore 1 Answer

Game Manager can't decide what the instance of the object is despite just making an instance of it 1 Answer

Getting 'Object Reference not set' when calling function from instantiated object 1 Answer

"I am getting a null reference exception error for Pathfollowertest.instance.centerslot.Add(Cube.transform) 0 Answers

NullReferenceException when referencing to another class 2 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