• 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 /
This question was closed Oct 07, 2016 at 08:14 AM by RangerDog for the following reason:

Noob question from my side XD

avatar image
Question by RangerDog · Sep 15, 2016 at 01:33 PM · variablereference

Referencing floating variables from another script? C#

So i am trying to get ID from the script "ContainerController", to ContainerTrigger". I know you have to do it with GetComponent, but it returns the error:

NullReferenceException: Object reference not set to an instance of an object ContainerTrigger.OnMouseDown () (at Assets/Prefabs/Containers/Scripts/ContainerTrigger.cs:9) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

The script is for if you click a spawned container, which are spawned with a for loop, you get the ID in the log. If i make a temp ID in the trigger script, it works fine. So yeah, im just not sure what im doing wrong with the GetComponent here XD (Yeah, fairly new to C# =p)

ContainerController:

 using UnityEngine;
 using System.Collections;
 
 public class ContainerController : MonoBehaviour {
 
     public GameObject[] containers1; 
     public GameObject[] containers2; //So you can (for example) Put dirty, open etc. Containers in that area
     private int Pos1y;
     private int Pos1x;
     private int Pos1z;
     private int Pos2y;
     private int Pos2x;
     private int Pos2z;
     private int ContRotx = -90;
     public float id;
 
     void Start()
     {
         //Spawning Containers at Position 1
         for (Pos1y = 2; Pos1y < 20; Pos1y += 4)
         {
             for (Pos1x = 0; Pos1x < 45; Pos1x += 12)
             {
                 for (Pos1z = 50; Pos1z < 60; Pos1z += 4)
                 {
                     GameObject container1 = containers1[Random.Range(0, containers1.Length)];
                     Instantiate(container1, new Vector3(Pos1x, Pos1y, Pos1z), Quaternion.Euler(ContRotx, 0,0));
                     id = id + 1;
                 }
             }
         }
 
         // Spawning at Another position, with a different set of containers.
         for (Pos2y = 2; Pos2y < 30; Pos2y += 4)
         {
             for (Pos2x = 0; Pos2x < 30; Pos2x += 12)
             {
                 for (Pos2z = 0; Pos2z < 30; Pos2z += 4)
                 {
                     GameObject container2 = containers2[Random.Range(0, containers2.Length)];
                     Instantiate(container2, new Vector3(Pos2x, Pos2y, Pos2z), Quaternion.Euler(ContRotx, 0, 0));
                     id = id + 1;
                 }
             }
         }
 
         // Trying for diff pos, fail or no? idk!
         for (Pos2y = 2; Pos2y < 30; Pos2y += 4)
         {
             for (Pos2x = 0; Pos2x < 30; Pos2x += 12)
             {
                 for (Pos2z = 100; Pos2z < 130; Pos2z += 4)
                 {
                     GameObject container2 = containers2[Random.Range(0, containers2.Length)];
                     Instantiate(container2, new Vector3(Pos2x, Pos2y, Pos2z), Quaternion.Euler(ContRotx, 0, 0));
                     id = id + 1;
                 }
             }
         }
     }
 }


Annd ContainerTriger:

 using UnityEngine;
 using System.Collections;
 
 public class ContainerTrigger : MonoBehaviour
 {
     void OnMouseDown() //When Container Clicked, it shows something in the debug log :D
     {
         ContainerController containerController = GetComponent<ContainerController>();
         Debug.Log(containerController.id);
     }
 }

Also it works fine if i spawn the ID's straight from the containercontroller, but thats not what i want :p Small edit: The ContainerController does have the tag "ContainerController".

Comment

People who like this

0 Show 5
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 Hellium · Sep 15, 2016 at 12:40 PM 1
Share

FAQ :

Some reasons for getting a post rejected:

  • Posting about a specific compiling error or NullReferenceException: there is a myriad of these questions with answers already, have a look at those posts to get hints on what could possibly be your issue. Also, take a look at the Unity's support website, many errors and how to solve them are described here

avatar image RangerDog · Sep 15, 2016 at 01:41 PM 0
Share

Hey, Well the problem is, I wouldn't have ANY idea at all what to Put there. Its a public float since it said in another answer, but it doesnt say what i need to put into it?

avatar image NerdClown RangerDog · Sep 15, 2016 at 01:55 PM 1
Share

GetComponent looks in the component that are attached to the game object that the script is on. To find stuff in scene you'll have to do a Find (but don't do them too often, they're expensive) or something else ... like a reference or the singleton pattern or so (there's an example of the latter HERE ).

If you wanna debug it, try something like this:

 ContainerController containerController = GetComponent<ContainerController>();
 if(containerController == null)
   Debug.Log("ContainerController not found!!!");

avatar image TBruce · Sep 15, 2016 at 06:48 PM 1
Share

As stated in the error message you are attempting to use a null variable (in this case containerController).

The reason containerController is null is because that component is not attached to the same GameObject that ContainerTrigger is. First you want to move this

 ContainerController containerController = GetComponent<ContainerController>();

from the OnMouseDown() function and add it to a Start() function. But you stiil need to have a way of getting a reference to the ContainerController. And how you do this depends on where the reference to the ContainerTrigger is.

At a minimum you can change ContainerTrigger to this

 using UnityEngine;
 using System.Collections;
 
 public class ContainerTrigger : MonoBehaviour
 {
     private ContainerController containerController = null;;
     void Start ()
     {
         GameObject go = GameObject.FindWithTag("ContainerController");
         if ((go != null) && (go.GetComponent<ContainerController>() != null))
         {
             containerController = go.GetComponent<ContainerController>();
         }
     }
 
     void OnMouseDown() //When Container Clicked, it shows something in the debug log :D
     {
         if (containerController != null)
         {
             Debug.Log(containerController.id);
         }
     }
 }

avatar image RangerDog · Sep 16, 2016 at 06:54 AM 0
Share

In the end, it was actually a stupid mistake from me. I had to turn it around, make the Variable in the Controller script, and the ContainerController containerController = GetComponent(); In every foor loop, and change that to: ContainerTrigger containerTrigger = container1.GetComponent();

Thanks for the help tough!

0 Replies

  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pass reference to variable in IEnumerator method in C#? 2 Answers

Change variable independently for different objects with same script.,Change variable in different object but same script 0 Answers

Why is refrence to parent variable not updating 1 Answer

Why is my referenced variable changing values? 1 Answer

What are some best practices for static variables in multiplayer? 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