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 gordeszkakerek · May 13, 2012 at 10:16 PM · objectenableactivateactivationanother

How to activate another Object with java script?

I have a GUIButton and a Object, name: "Car". I will activate the car with the GUIButton. GUIButton have this script:

 function OnGUI()
 {
     GUI.enabled = true;
     if(GUI.Button(new Rect(0, 0, 200, 100),"Touch here to activate Car."))
     {
         gameObject.active = false;
     }
 }

I know the "gameObject.active" it's not good here, but how can activate my "Car" Object?

Comment
Add comment · Show 1
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 JG_2 · Jun 14, 2013 at 06:25 PM 0
Share

I´ve the same problem. And I´m sorry I´m a newbie too so I don´t have an answer. And sorry for my english I´m a german pupil :D

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · May 13, 2012 at 10:49 PM

Have at the top of your script:

 public var car : GameObject;

this will create a slot in the script inspector that you can drag the car onto. Then,

 car.active = false;

will deactivate the car!

Comment
Add comment · 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
0

Answer by theabsinthehare · May 13, 2012 at 11:03 PM

By "activate" do you mean enable/disable the entire object, or do you want to access a function on the object "Car"?

If you want to access a function, you would reference the car via a GameObject variable in the script with the GUI function, then use GetComponent to access the script and function you want.

 private var carVariable : GameObject;

 function Start () 
 {
     carVariable = GameObject.Find("Car")
 }


 function OnGUI()
 {
     GUI.enabled = true;

     if(GUI.Button(new Rect(0, 0, 200, 100),"Touch here to activate Car."))
     {
         carVariable.GetComponent(NameOfScriptOnCar).NameOfFunction(param1, param2, etc);
     }
 }
Comment
Add comment · Show 1 · 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 syclamoth · May 13, 2012 at 11:05 PM 1
Share

But don't use GameObject.Find(string), there's never a reason to do that except for laziness.

avatar image
0

Answer by bompi88 · May 13, 2012 at 11:01 PM

An example. Drag your Car object to the car placeholder in the inspector.

    var Car : GameObject;
     
     // this should probably be in your car script or somewhere else, 
     // I don't know what you actually are trying to do here.
     function Awake () {
        Car.active = false;
     }
     
     function OnGUI()
     {
     
        GUI.enabled = true;
     
        if(GUI.Button(new Rect(0, 0, 200, 100),"Touch here to activate Car."))
        {
            gameObject.active = true;
        }
     
     }
Comment
Add comment · 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
0

Answer by gordeszkakerek · May 16, 2012 at 04:13 PM

Thanks for all answer. Unfortunately it's not work for me. Maybe I wrote someting bad in the question. Sorry! So more simply: I want activate a Object with GUI button: 1. Firt the Object is deactive(disable or was...) 2. Player collusion a Trigger, Trigger activate a GUI button. 3. If I click GUI button, the Object is active. GUI button activate the Object.

Please help.

Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Deactivate A Parent (And Its Children)? 4 Answers

turn on a game object? 1 Answer

SetActive won't work 3 Answers

how to enable/disable slider VIA script from a prefab 0 Answers

Activate and deactivate some script when i press a Key. 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