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 Grug · Jan 24, 2012 at 03:49 AM · getcomponentcomponenttag

Cannot disable a script from another script

I have been trying to find the solution for the past hour. I have a chicken enemy in my Game. One script, "Randomnpcmovement" controls the movement and attacking. The other, "generictakedamage" controls health and death. I am trying to use "generictakedamage" to disable "Randomnpcmovement" once health is at zero or less. But for all the combinations of syntax I have tried, it keeps getting rejected by Unity. Both scripts are in the same object called "TempChicken". The entire content of "generictakedamage" is as follows:

 var health : float = 300;
 var go = Randomnpcmovement.Find(
 var randomMovement: Randomnpcmovement = GetComponent(Randomnpcmovement);
 
 function Start (){
 }
 
 function OnCollisionEnter(whatIHit : Collision)
 { 
     health -= 10; 
     print ("Chickenshot");
 }
 
 function Update (){
     if (health <= 0) {
         randomMovement.GetComponent("Randomnpcmovement").enabled = false;
      }
 }
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 syclamoth · Jan 24, 2012 at 03:55 AM 1
Share

Also, next time post more cleanly. I've fixed it up for you this time, but I can't do it for you all the time. Remember that when you are writing a question, it gives you a preview below the text-entry field. You can use this to make sure that what you are posting looks right.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by syclamoth · Jan 24, 2012 at 03:54 AM

The problem here is where you are using the GetComponent function. You can't use lookup functions that depend on the current state of the object in class-variable declarations, because they need to be evaluated at compile-time, not at runtime!

You should get rid of the 'go' variable from the top, and instead of trying to find the component for RandomMovement during field initialisaion, do it in Start:

private var randomMovement : randomnpcmovement;

function Start() { randomMovement = GetComponent(randomnpcmovement); }

Then when you want to access it in Update, just use randomMovement:

function Update (){
    if (health <= 0) {
        randomMovement.enabled = false;
    }
}
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 Grug · Jan 24, 2012 at 04:02 AM 0
Share

I have done as you asked. Now I am receiving the error:

Assets/generictakedamage.js(2,30): BCE0018: The name 'Randomnpcmovement' does not denote a valid type ('not found'). Did you mean 'UnityEngine.RuntimePlatform'?

I'm unsure how to proceed.

avatar image syclamoth · Jan 24, 2012 at 04:06 AM 1
Share

Well, that's up to you. $$anonymous$$ake sure that the filename of the script you are trying to get is the same as the name in the GetComponent function- I can't really help you if you have spelled the names wrong in your original question post!

avatar image syclamoth · Jan 24, 2012 at 04:07 AM 0
Share

(without the .js at the end, obviously)

avatar image Grug · Jan 24, 2012 at 04:14 AM 0
Share

I double checked and both are spelled correctly. Here's a screenshot proving it: http://imgur.com/nigZx

avatar image Lo0NuhtiK · Jan 24, 2012 at 05:07 AM 0
Share

alt_Text
Fix that stuff at the top of your script where you declare the variable 'random$$anonymous$$ovement' twice (once as private, the next with GetComponent)... make only ONE random$$anonymous$$ovement variable and only set it as variable : ScriptName ; ..then in start set it with the getcomponent like @syclamoth showed above.

Then, take a closer look at your image in the inspector panel.
Next to the checkbox for enabling the script, it's showed as "Randomnpcmovement" ...BUT... directly beneath that in the script slot, your script is actually named "randomnpcmovement" with all lower-case letters. The editor displays some things differently for easier reading. $$anonymous$$ake sure you're using the actual script name.

 var random$$anonymous$$ovement : randomnpcmovement ;
 function Start(){
    random$$anonymous$$ovement = GetComponent(randomnpcmovement) ;
 }



Look at your Generictakedamage display in that same image.. you've also named that script with all lower-case and would have caused the same problem you're having here. It's better practice to capitalize ScriptNames anyway. RandomNpc$$anonymous$$ovement ; GenericTakeDamage .
Show more comments
avatar image
0

Answer by Grug · Jan 24, 2012 at 05:39 AM

This is for a project that is due tomorrow. I would appreciate help, especially since it seems to be a Unity problem.

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 · Jan 24, 2012 at 06:27 AM 0
Share

No, it's definitely your fault. See the comment at the end of my answer. It's a spelling mistake. Spelling is very important when you're program$$anonymous$$g.

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

What is the component name above transform? 1 Answer

How do I find an external component from inside a class? 0 Answers

Acessing a Script Instance from Another Object 3 Answers

Adjusting size of a halo in C# 0 Answers

Why is reference to script object == null? 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