- Home /
Get a script to the object with the press of a button
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");
}
}
Answer by syclamoth · Oct 27, 2011 at 09:02 AM
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-
SplashScreenthecave screen;
void Update() {
if(Input.GetKeyDown("e"))
{
screen = gameObject.GetComponent<SplashScreenthecave>();
}
}
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?
well, since it still won't work what I hoped for...I should tell you.
the idea here is that I found a good script to make transition between scenes on the unity wiki(search for Splash Screen). It works great, but the problem is that it automatically enters a new scene as soon the script is attached to the camera. So I tought that I could fix that when you press the E button, you both get a good transition and goes to the next scene.
but as I can see, it doesn't work =/
Ah. The problem is, GetComponent doesn't add a new component to your object- it gets a reference to an existing one! You should be using
AddComponent<Type>();
ins$$anonymous$$d.
Also, I don't like the look of that 'thecave' suffix on the type name. Are you sure you're going about this in the best way? The splash-screen class provides options for choosing the information which gets displayed, and the level which gets loaded- AddComponent can be used in the same way as I was using GetComponent above to return a reference- you can use that to set up the information.
Better yet, you could have a prefab with all the correct settings (one for each level), which you instantiate! Then you don't have to worry about all this componentry messing around.
Another thing- looking over that script, no it doesn't start straight away. If you turn off the 'startAutomatically' boolean in the inspector, you can then activate it using the line
GetComponent<SplashScreen>().StartSplash();
from any script on the same object.
tried the prefab solution
It works! thanks man! : D
altough I get an error message: The object of type 'Camera' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. SplashScreenlevel1.OnGUI () (at Assets/Scripts/SplashScreenlevel1.cs:127)
but anywho, it's working now. Thanks again!
Yeah, it's a known bug, it says so on the page. Not exactly critical, since it only goes for less than a frame.
Your answer
Follow this Question
Related Questions
Personaje cambie de sprite al teleportarse [Characters change sprite to teleport] 0 Answers
script GetComponent, nullReference error 1 Answer
I Made a Revolver Script, But It's Saying There is an Error 2 Answers
reference an object without pre-assigning. 2 Answers
Can't modify variable from another gameobject script 0 Answers