Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Novrick · Oct 27, 2011 at 08:52 AM · inputgetcomponent

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");    
         
     }
     
     
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Novrick · Oct 27, 2011 at 09:14 AM 0
Share

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 =/

avatar image syclamoth · Oct 27, 2011 at 09:29 AM 0
Share

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.

avatar image syclamoth · Oct 27, 2011 at 09:33 AM 0
Share

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.

avatar image Novrick · Oct 27, 2011 at 11:38 AM 0
Share

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!

avatar image syclamoth · Oct 27, 2011 at 02:15 PM 0
Share

Yeah, it's a known bug, it says so on the page. Not exactly critical, since it only goes for less than a frame.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges