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 oliver-jones · Mar 31, 2011 at 01:56 PM · prefabactivatedeactivate

How To Disable A Prefab?

Hello,

I have a GUI click and drag - basically, you can click on a GUI button of, say a turret and a turret prefab is initiated on the mouse position and you can drag it onto the scene somewhere - the problem is, is when I drag it around the scene, its already active - in other words, its shooting the enemy.

I want it to be active when the user clicks (places the turret) - so is there a way to deactivate all the scripts on that particular prefab, and then activate it when the user clicks?

function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; var layerMask = 1 << 8; if (chooseLocation) { layerMask = ~layerMask; Physics.Raycast (ray, hit, 500, layerMask);

     currentObj.Translate((Vector3(hit.point.x, currentObj.position.y, hit.point.z) - currentObj.position) * Time.deltaTime *followSpeed);
 }
 if (Input.GetButtonDown("Fire1")){ //Input.GetMouseButton(0)
     chooseLocation = false;
 }

 // Cancel Selection \\
 if(Input.GetButtonDown("Fire2") &amp;&amp; currentObj.gameObject){
     objLimit[currentSelected] ++;
     chooseLocation = false;
     Destroy(currentObj.gameObject);
 }

}

function OnGUI () {

 selGridInt = 0;
 if(chooseLocation == false){
     if (GUI.Button(Rect(10, 10, 120, 30), objItemNames[selGridInt] + " ("+objLimit[selGridInt]+")") &amp;&amp; objLimit[selGridInt] &gt; 0){
         currentSelected = selGridInt;
         chooseLocation = true;
         objLimit[selGridInt] --;
         currentObj = Instantiate (objGroup[selGridInt],Vector3(0,0,0),Quaternion.identity);
         currentObj.gameObject.GetComponent("BoxCollider");
         currentObj.position.y = groundPlane.position.y + 0.5;


     }
 }

Thanks

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 efge · Mar 31, 2011 at 02:20 PM

You could use Input.GetButtonUp to set an boolean variable true and create an "overall" if-statement in the function Update for your turret script:

if (Input.GetButtonUp(...))
  isReady = true;

and

function Update () {
  if (isReady) {
    ...
  }
}


Or you could disable/enable that script like any other component:

var script : ScriptName;
script = GetComponent("ScriptName");
...
if (Input.GetButtonUp(...))
  script.enabled = 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 e-bonneville · Mar 31, 2011 at 02:21 PM

Well, you just need to set a boolean variable (true/false variable) to true when you place the turret down. You can use an if statement to check if the variable is false, then make it so the turret won't shoot if it is. You don't need to completely disable the script.

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

No one has followed this question yet.

Related Questions

Using a pool of objects that will be spawned ONCAMERA and removed OFFCAMERA instead of INSTANTIATE & DESTROY (iOS) 1 Answer

Activating & Deactivating prefabs from a prefab pool VS simply moving them on and off-camera (iOS) 1 Answer

Unchecking a prefab's script in the Inspector doesn't "deactivate" it? 1 Answer

How to activate and deactivate a gameObject 1 Answer

How to activate/deactivate a component(Animator) when a specific game object was activated 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