• 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
0
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, w$$anonymous$$ch 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 somet$$anonymous$$ng 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
Add comment · 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
avatar image RangerDog · Sep 15, 2016 at 01:41 PM 0
Share
avatar image NerdClown RangerDog · Sep 15, 2016 at 01:55 PM 1
Share
avatar image TBruce · Sep 15, 2016 at 06:48 PM 1
Share
avatar image RangerDog · Sep 16, 2016 at 06:54 AM 0
Share

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

What are some best practices for static variables in multiplayer? 0 Answers

Component refrence goes null randomly? 3 Answers

Access Main Camera and store it in a variable for quick access? 2 Answers

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


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