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 RussianBsack · Oct 07, 2014 at 11:13 PM · activatescript loading

How to activate a specific script in a Prefab.

 using UnityEngine;
 using System.Collections;
 
 public class playselection : MonoBehaviour {
 
     public Component moveScript;
 
     // Update is called once per frame
     void Update () {
 
         if (Input.GetMouseButtonDown(0))
         {
             moveScript.enabled = true;
         }
 
         else
         {
             moveScript.enabled = false;
         }
     
     }
 }
 

I have been trying to active a specific script that is located within the object (cube move) but for whatever reason it is not letting me do that. This is the script that is on the GUI texture, i want it so that once the GUI text is pressed, the cube move script is activated and the controls of the player resume.

Once they have resumed, i need the gui text to delete (destroy).

Help, please!

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

2 Replies

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

Answer by robertbu · Oct 07, 2014 at 11:16 PM

There is no 'enabled' property in a component. Usually the the hierarchy is:

   Object-->Component-->Behaviour-->MonoBehaviour-->YourScript

The enabled flag is in Behaviour class, so you can make 'moveScript' of any of the types above component. Typically I see either MonoBehaviour, or YourScript.

Comment
Add comment · Show 2 · 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 RussianBsack · Oct 07, 2014 at 11:19 PM 0
Share

Could you explain a little further? I get what you are saying, but how would i go about doing it? Putting the script into an empty gameObject?

avatar image robertbu · Oct 07, 2014 at 11:50 PM 0
Share

Based on your comment Chemo, maybe we are going about this the wrong way. Based on your example code, I assume you had a script on a prefab. And once that prefab is Instantiated() into the scene, you wanted to enable that script. But maybe what you want to do is enable the game object. If so, then 'Component' should be GameObject, and the call should be moveScript.SetActive(true).

Note both my original answer assumes that the above script is on some object in the prefab, and that moveScript has been initialized by drag and drop. Note the above script must be on an active game object. A script does not Update() messages when the game object is deactivated.

avatar image
0

Answer by Cherno · Oct 07, 2014 at 11:31 PM

If by "Prefab", you mean an instantiated gameObject that is actually in your scene:

Change the word "Component" in line

 public Component moveScript;



to the actual class name of the moveScript. So, if your script has the name MoveScript.cs, the line would be:

 public MoveScript moveScript;
Comment
Add comment · Show 2 · 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 RussianBsack · Oct 07, 2014 at 11:42 PM 0
Share

I did that, but it doesn't do anything. It doesn't activate the Cubemove script that is located on another object.

 using UnityEngine;
 using System.Collections;
 
 public class playselection : $$anonymous$$onoBehaviour {
 
     public Component moveScript;
 
     // Update is called once per frame
     void Update() 
     {
         if(Input.Get$$anonymous$$ouseButtonDown(0)) 
         {
             moveScript.GetComponent<Cubemove>().enabled = true;
 
         }
 
     }
 
 }
avatar image Cherno · Oct 07, 2014 at 11:53 PM 0
Share

The moveScript of course HAS to be the $$anonymous$$oveScript script that is on the gameObject in question, it can't just be a script on a Prefab that sits in the project folder. Just to be clear because I don't know what you mean by "another object".

Also, your script enabling code is wrong now.

  moveScript.GetComponent<Cubemove>().enabled = true;

would mean that the moveScript has another Component on it called Cube$$anonymous$$ove... which it doesn't because it is itself a Component on a gameObject.

The line has to be

  moveScript.enabled = true;

Just as before, but you have to change to initial declaration to what I have written before.

 public $$anonymous$$oveScript moveScript;

So since the script in question seems to be called "Cubemove", the line would have to be

 public Cubemove moveScript;

I don't think you can drag components into an inspector field to assign the variable. so you would have to do it by code, like

 moveScript = theRightGameObject.GetComponent<Cubemove>();
 
 
 
 

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Application.LoadLevel overwrote my scene? 1 Answer

Activate and Deactivate Animations 1 Answer

Script for buttons referencing different Game Objects 0 Answers

Object won't reactivate or activate with button press. 2 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