Noob question from my side XD
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".
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
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?
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!!!");
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 On$$anonymous$$ouseDown() 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 $$anonymous$$imum you can change ContainerTrigger to this
using UnityEngine;
using System.Collections;
public class ContainerTrigger : $$anonymous$$onoBehaviour
{
private ContainerController containerController = null;;
void Start ()
{
GameObject go = GameObject.FindWithTag("ContainerController");
if ((go != null) && (go.GetComponent<ContainerController>() != null))
{
containerController = go.GetComponent<ContainerController>();
}
}
void On$$anonymous$$ouseDown() //When Container Clicked, it shows something in the debug log :D
{
if (containerController != null)
{
Debug.Log(containerController.id);
}
}
}
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!