hey, I’m trying to call a script to my object by pressing the E button…but it doesn’t work =/
I work in C#.
here’s the script:
using UnityEngine;
using System.Collections;
public class realitycamera : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey("e"))
{
gameObject.GetComponent("SplashScreenthecave");
}
}
The problem here is that gameObject.GetComponent(“SplashScreenthecave”) returns a reference to a script named SplashScreenthecave - but you aren’t doing anything with it, you aren’t even assigning it to anything. If you wanted to save a reference to it so that you could do something to it later, you could do this-
Now, what I don’t understand here, is why you are doing this at all! Is there any reason why you can’t just define ‘screen’ at the top as a public variable, then assign it in the inspector? gameObject.GetComponent() will always return the same object reference as long as you aren’t changing the components on your object. If you remove the component, it will return null, which is no use to you, and if you have more than one it will only ever return one of them- and I’m not actually sure which one. What is the intended purpose of this line?