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 trvr · Apr 27, 2013 at 06:51 PM · javascriptgraphicsenablevehicle

Enable a disabled gameobject via javascript?

Hi all. I am trying to build a simple system for entering and exiting vehicles. Currently both the player and the vehicle are the default FPS controllers. I am able to run up to the vehicle controller, enter it, disable the player controller, and drive the vehicle. However, I cannot re-enable the disabled player controller. Any and all help is appreciated. Here is the code I am using, it is applied to the vehicle, which has a trigger for range sensing. Here is my code


 #pragma strict
 
 var inRange = false;
 var inCar = false;
 //This is the player that enters the vehicle:
 var player : Transform;
 //This is the player camera:
 var playerCamera : Camera;
 //This is the Vehicle:
 var Vehicle : Transform;
 //This is the vehicle camera:
 var vehicleCamera : Camera;
 //These are the control scripts for your vehicle:
 private var lookScript : MouseLook;
 private var moveScript : CharacterMotor;
 private var inputScript : FPSInputController;
 
 function Start () {
     playerCamera.enabled = true;
     vehicleCamera.enabled = false;
         //These are the vehicle control scripts.
     lookScript = GetComponent(MouseLook);
     moveScript = GetComponent(CharacterMotor);
     inputScript = GetComponent(FPSInputController);
 }
 
 function Update () {
     if(Input.GetButton("Enter") && inRange == true) {
         if(inCar == false) {
             inCar = true;
             //Switch Cameras
             playerCamera.enabled = false;
             vehicleCamera.enabled = true;
             //Parent player to Vehicle
             player.parent = Vehicle.transform;
             player.transform.localPosition = Vector3(-1.5,0,0);
             //Make Player Invisible
             player.gameObject.SetActive(false);
             //Enable Vehicle Control Scripts
             lookScript.enabled = !lookScript.enabled;
             moveScript.enabled = !moveScript.enabled;
             inputScript.enabled = !inputScript.enabled;
         }
     }
     else if(Input.GetButton("Enter") && inCar == true) {
         //inCar = false;
         //Switch Cameras
         playerCamera.enabled = true;
         vehicleCamera.enabled = false;
         //Unparent player to Vehicle
         player.transform.parent = null;
         //Make Player Visible
         player.gameObject.SetActive(true);
         //Enable Vehicle Control Scripts
         lookScript.enabled = !lookScript.enabled;
         moveScript.enabled = !moveScript.enabled;
         inputScript.enabled = !inputScript.enabled;
     }    
 }
 function OnTriggerEnter (other : Collider) {
         inRange = true;
         Debug.Log("InRange!");
 }
 function OnTriggerExit (other : Collider) {
         inRange = false;
 }



If anyone can help me figure this out, that would be amazing. Thank you in advance.

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 trvr · Oct 28, 2013 at 04:41 AM 0
Share

So close to getting a badge...

1 Reply

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

Answer by Fattie · Apr 27, 2013 at 08:59 PM

Do NOT use this format

lookScript.enabled = !lookScript.enabled;

just set them explicitly:

lookScript.enabled = false; (or whatever the case is)

in each of the six lines of code like that.

Comment
Add comment · Show 4 · 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 EG-Hernandez · May 21, 2013 at 02:48 AM 0
Share

@Fattie, a little off topic but, how would I setActive a child GO, the becomes inactive at the start, but I want to become active again after a collision event?

avatar image Fattie · May 21, 2013 at 06:51 AM 0
Share

nowadays sit is SetActive(false); and SetActive(true);

so just add a one-line script TO the child object that sets it not active, in Start()

in the PARENT script, have

var theChildOfInterest:GameObject

and connect it to that.

after the collision (or whenever you want) just set it active, theChildOfInterest.SetActive .. etc.

avatar image EG-Hernandez · May 21, 2013 at 06:46 PM 0
Share

YES!! I got it to work, thank you. :D

avatar image Fattie · May 21, 2013 at 09:11 PM 0
Share

lol GREAT !!

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

14 People are following this question.

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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Problem with the GUISkin on the script 1 Answer

background picture in my game 1 Answer

Is this a problem of unity 3 or i am doing something wrong. 1 Answer


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