- Home /
Cache GetComponent correctly in C#
Here is my problem. Button on wall has button script. Elevator platform has Elevator script (everything is C#) When Button is pushed it needs to call GoToFloor in the Elevator script.
I cobbled together this from other examples on Unity Answers.
How do I make the result of GetComponent Public so every function in Button.cs can use it? Currently it is resisting my attempts to use GetComponent in one place and save it for use somewhere else without calling GetComponent AGAIN (which I have read you only want to do once for efficiency sake:
CODE:
Elevator.cs using UnityEngine; using System.Collections; using UnityEngine; using System.Collections;
public class Elevator : MonoBehaviour {
public Transform target;
private float initialx = 159 ;
private float initialy = 0.2F ;
private float initialz = 821.4F;
private float floor1Height = 0F;
private float floor2Height = 10F;
private int floorNum;
private float elevatorSpeed ;
private Vector3 velocity = Vector3.zero;
private float smoothTime = 0.3F;
private float floor3Height ;
private GameObject elevator;
private bool elevatorActive = false;
private float elevatorDestination ;
void Start()
{
// initialise variables which must have a value, otherwise errors ensue
elevator = this.gameObject;
elevatorActive = false;
elevatorDestination = elevator.transform.position.y;
}
void Update() {
if(elevatorActive)
{
MoveElevator();
}
}
void MoveElevator()
{
// Move the elevator
Vector3 targetPosition = target.TransformPoint(new Vector3(initialx,initialy + elevatorDestination,initialz));
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
//elevator.transform.position.y = Mathf.Lerp(elevator.transform.position.y, elevatorDestination, elevatorSpeed * Time.deltaTime);
// Disable the elevator if it is within a very small margin of the destination, ie it is 'close enough' so stop
//if(Mathf.Abs(elevator.transform.position.y - elevatorDestination) < 0.01)
//{
// print("stopped elevator");
// elevatorActive = false;
//}
}
public void GoToFloor(int floorNum)
{
switch(floorNum)
{
case 1:
elevatorDestination = floor1Height;
break;
case 2:
elevatorDestination = floor2Height;
break;
case 3:
elevatorDestination = floor3Height;
break;
default:
elevatorDestination = floor1Height;
break;
}
elevatorActive = true;
}
void StopElevator()
{
elevatorActive = false;
}
void ResumeElevator()
{
elevatorActive = true;
}
}
BUTTON CODE:
using UnityEngine;
using System.Collections;
public class Button : MonoBehaviour { public GameObject target; public bool pushed = false; //public myelevator; (does not work)
void Start() {
//GameObject myelevator = GameObject.Find("elevator");
Elevator myelevator = GetComponent<Elevator>(); // How do I export myelevator so it can be used in OnMouseUp()?
// Doing a GetComponent once is the best idea I understand, but how do I save it for other functions?
//myelevator bar = myelevator.GetComponent<GoToFloor()>();
}
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;
//Elevator.elevator.GoToFloor(2);
myelevator.GoToFloor(2); // myelevator from above not recognized! why?
Debug.Log("Button Pushed.");
}
void Done() {
pushed = false;
}
}
Answer by illwunn · Aug 15, 2011 at 09:26 PM
Why would u not just make the elevator refrence a global variable? Like this:
Elevator myElevator;
void Start() {
myElevtaor = GetComponent<Elevator>();
}
that way you can access it anywhere in the button script?... or am i missing sumthing?
It needs the public
keyword but this is the right way to cache a script reference.
ya sorry, if u wanted to access it outside of the button class jus make the variable public
Answer by cernst77 · Aug 15, 2011 at 09:31 PM
I wanted to try this but, being a newbie, I was probably botching the syntax of what you have. Visual Studio was red underlining it. I have to pack up and go home before I try what you have there, I'll try it when I get home and see if your syntax works, thanks.
Just a reminder, I need to be able to access GoToFloor on Elevator, that declaration will work for that?
yep, as long as the GoToFloor function is public you can jus type in myElevator.GoToFloor(); and that shud call the function properly
Please don't use Answers to expand your Question or to reply to an Answer. Editing your question and adding new pertinent information is the correct way to let people know there is more you need to know. Answers are not sorted chronologically because they are not meant to be used as a forum.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
how can i add another scripts functionality to a button on another script? 2 Answers
Multiple Cars not working 1 Answer
Button's sprite swap works fine but it doesn't change the related image's source image! why? 1 Answer
Change button icon for each without multiple GUISkins 1 Answer