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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by carter-carl30 · Apr 17, 2012 at 09:59 PM · scriptingbasicsshootactivatedeactivate

how to activate/de-activate different scripts on a gameobject?

Hi all,

I am trying to learn unity and some scripting on the way. I have a ship with a shoot script on it. What I can't figure out is how to (when I collide with a powerup) to turn off the shoot script and activate say a laser script (shoot script with different bullet prefab)

I am currently doing this by having 2 seperate prefabs each with the different shoot scripts on that instantiate on a "mount" when the power up is collided with (this seems to work but not all the time).

It would be easier if i could have the 2 scripts on my ship and when i hit the power up it turns the basic gun script off and turns the laser script on. Can anyone help me with this 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
2
Best Answer

Answer by programmrzinc · Apr 17, 2012 at 10:06 PM

  var HOLDER : GameObject; //Holds BOTH scripts

   function //Whatever () {

       HOLDER.GetComponent(//Name Of Script 1).Enabled = False or True;

       HOLDER.GetComponent(//Name Of Second Script).Enabled = False or True;

}

You have to tweak it to your specifications. Sorry:)_

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 carter-carl30 · Apr 17, 2012 at 11:06 PM 0
Share

I've tried this (will add to my ship object):

var HOLDER : GameObject; //Holds BOTH scripts

function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "spiral_laser_powerup") {

   HOLDER.GetComponent(Shootspherescript).Enabled = False;

   HOLDER.GetComponent(Shoot_spiral_laser_script).Enabled = True;

} }

I'm getting the error "BCE005: $$anonymous$$ identifier: 'False'. any ideas?

avatar image T27M · Apr 17, 2012 at 11:09 PM 0
Share

False should be false. Same with True should be true.

avatar image carter-carl30 · Apr 17, 2012 at 11:14 PM 0
Share

now i'm confused hehe, is it something i need to add under the first line of code? eg False=False and True=True ??

avatar image T27M · Apr 17, 2012 at 11:15 PM 0
Share

Sorry should have made it more clear. You dont need a capital letter for false. False and false are totaly different things.

something.enabled = false; something.enabled = true;

avatar image T27M · Apr 17, 2012 at 11:19 PM 0
Share

I was actually going to write a script out to help you but for some reason unity just doesn't want to work today. Had to restart it a dozen times might reinstall it.

Show more comments
avatar image
1

Answer by Fabkins · Apr 18, 2012 at 12:32 AM

I would use a different method. I would attach the multiple scripts to the object. Each script would have the same function calls, in this case "fire".

From the parent script:

 function Update () {
 // do your logic....
 
 gameObject.SendMessage("fire","laser");
 gameObject.SendMessage("fire","blaster");
 }

And then each script can check to see if its being called. Like so:

 function fire(type: String)
 {
 if(type=="laser")
     {
     Debug.Log("Fire weapon laser");
     }
 }

The handy thing about do this rather than turning the script off is that if the weapon has a cooldown timer then the script can carry on doing its thing.

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 carter-carl30 · Apr 18, 2012 at 05:56 PM 0
Share

I now have this script (see below). I am getting this error: $$anonymous$$issingFieldException: Field 'Shootspherescript.Enabled' not found.

var HOLDER : GameObject; //Holds BOTH scripts var orb_mount : GameObject; //object to spawn item at var orbs : GameObject; //powerup spawned

function Start () {

   HOLDER.GetComponent(Shootspherescript).Enabled = true;

   HOLDER.GetComponent(Shoot_spiral_laser_script).Enabled = false;

}

function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "spiral_laser_powerup") {

   HOLDER.GetComponent(Shootspherescript).Enabled = false; <-error

   HOLDER.GetComponent(Shoot_spiral_laser_script).Enabled = true;

}

 if (other.GameObject == "orb_power_up")
 {

 var orbs : GameObject = Instantiate(orbs, orb_mount.transform.position, orb_mount.transform.rotation);
 orbs.transform.parent = orb_mount.transform;
 }

}

avatar image Fabkins · Apr 18, 2012 at 06:32 PM 0
Share

Try:

HOLDER.GetComponent(Shootspherescript).enabled = false;

Lower case enabled.

avatar image carter-carl30 · Apr 18, 2012 at 07:31 PM 0
Share

@Fabkins, that fixed it thankyou! :)

avatar image carter-carl30 · Apr 18, 2012 at 08:22 PM 1
Share

thanks for everyone's help :)

my working script:

var HOLDER : GameObject; //Holds BOTH scripts

function Start () {

   HOLDER.GetComponent(Shootspherescript).enabled = true;

   HOLDER.GetComponent(Shoot_spiral_laser_script).enabled = false;

}

function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "spiral_laser_powerup") {

   HOLDER.GetComponent(Shootspherescript).enabled = false;

   HOLDER.GetComponent(Shoot_spiral_laser_script).enabled = true;
   yield WaitForSeconds (0.001);
   Destroy(other.gameObject);

}

}

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

6 People are following this question.

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

Related Questions

Activating/Deactivating GameObject Problem :( 3 Answers

Activating things- What on earth am I doing wrong??? 2 Answers

How to reset a variable when activated 1 Answer

Deactivating and Activating a group and all it's children? 1 Answer

How to deactivate gameObjects instead of destroying them ? 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