- Home /
Still strugging with GetComponent and accessing other objects (C#)
I am still struggling with nulls when trying to access a script on one object from a script on a completely different one.
I had asked this before, but had to put this task down for a couple weeks - went to revisit it and still struggled with no luck for hours even with the previous help I am still missing something.
I did manage to clean up my code and concept though in the mean time - still won't work though.
ERROR=NullReferenceException: Object reference not set to an instance of an object Button.OnMouseUp () (at Assets/Standard Assets/Scripts/SimHub CSharp/Button.cs:31) UnityEngine.SendMouseEvents:DoSendMouseEvents()
Offending line: (Button.OnMouseUp())
factoryLift1.GoToFloor(2); <-- something apparently null when we get here, why?PUBLIC MACHINE factoryLift1 up top should hold a value - but it will not take, what is my error anyone? please?
}
using UnityEngine; using System.Collections;
public class Button : MonoBehaviour {
public bool pushed = false;
public Machine factoryLift1;
void Start() {
GameObject tmp = GameObject.Find("Lift1");
if (tmp != null)
{
Machine factoryLift1 = tmp.GetComponent<Machine>();
}
}
void OnMouseEnter() {
//renderer.material.SetColor("_OutlineColor", Color.blue); .//not important right now
}
void OnMouseExit() {
//renderer.material.SetColor("_OutlineColor", Color.black); //not important right now
}
void OnMouseUp() {
pushed = true;
factoryLift1.GoToFloor(2);
}
void Done() {
pushed = false;
}
}
using UnityEngine; using System.Collections;
public class Machine : MonoBehaviour {
public Transform target;
private float floor1Height = 0F;
private float floor2Height = 10F;
private int floorNum;
public Vector3 myDestination;
private Vector3 velocity = Vector3.zero;
public bool isActive = false;
Vector3 floor1Destination; // start on ground
Vector3 floor2Destination;
// Use this for initialization
void Start()
{
isActive = false;
floor1Destination = target.transform.position; // original Vector3 Position - should be ground floor
myDestination = floor1Destination; // set floor 1 = ground floor and starting position vector3
Vector3 targetPosition = target.TransformPoint(new Vector3(0, 0, floor2Height)); // Calc new target vector3
floor2Destination = targetPosition; // New Vector3 Position
}
void Update() {
if(isActive)
{
MoveMe();
}
}
void MoveMe()
{
// Move the elevator
if (isActive == true)
{
float translation = Time.deltaTime * 2; // how smooth the motion is per update
// Elevator Up
target.Translate(Vector3.back * translation); //going up
if (target.transform.position.y > myDestination.y)
isActive = false; // Reached our floor
}
}
public void GoToFloor(int floorNum)
{
switch(floorNum)
{
case 1:
myDestination = floor1Destination;
break;
case 2:
myDestination = floor2Destination;
break;
default:
myDestination = floor1Destination;
break;
}
isActive = true;
}
void StopMe()
{
isActive = false;
}
void ResumeMe()
{
isActive = true;
}
}
Answer by aldonaletto · Sep 06, 2011 at 02:34 AM
The problem is in the line Machine factoryLift1 = tmp.GetComponent... : it's creating a temporary factoryLift1 variable inside Start, so the public factoryLift1 never sees the reference to the script Machine.cs. Remove Machine from this line and it should solve your problem:
... public bool pushed = false; public Machine factoryLift1;
void Start() {
GameObject tmp = GameObject.Find("Lift1");
if (tmp != null)
{ // remove this Machine before factoryLift1!
Machine factoryLift1 = tmp.GetComponent<Machine>();
}
}
Answer by cernst77 · Sep 06, 2011 at 02:43 AM
Thanks aldonaletto!
This works, Where is the thing on the page to mark this Answered? (Newbie here)
Your answer
![](https://koobas.hobune.stream/wayback/20220612202504im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object reference not set to an instance of an object in c#. 0 Answers
Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer
I'm having more problems accessing a variable from another script in c# 2 Answers
NullReferenceException when trying to access other GO in script. C# 1 Answer
Multiple Cars not working 1 Answer