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 question was closed Oct 19, 2014 at 09:43 AM by Griffo for the following reason:

Found another solution to the problem ..

avatar image
0
Question by Griffo · Oct 19, 2014 at 07:02 AM · getcomponentroot

OnBecameVisible() .. GetComponent(ScriptName)

Hi, I have a simple script (below) that I attach to EVERY enemy in the game to tell me if they are visible or not, the script is attached to each of the LOD renderer on each enemy.

My problem is accessing the root script, as each different kind of enemy has a different named script, so I would like this script to be able to look at the root gameObject and find the script itself, but I'm sure theres no way of doing that without using GetComponent(ScriptName) ..

Anyone know of a simple way of achieving this? Or have I got to put a copy of the script on every enemy with their script name in GetComponent?

Thanks.

 #pragma strict
 
 private var mainScript : SoldierGunSWAT_01;
 
 // used for testing
 private var k : int;
 
 // ------------------------------------
 function Start ()
 {
     mainScript = transform.root.gameObject.GetComponent(SoldierGunSWAT_01);
 }
 // ------------------------------------
 // Enables the behaviour when it is visible
 function OnBecameVisible()
 {
 // this yield need to be here else the OnBecameInvisible()
 // function below sets the variable to false as the other
 // LOD renderer on this model is swapped out
     yield WaitForSeconds(0);
     mainScript.soldierVisable = true;
 }
 // ------------------------------------
 // Disables the behaviour when it is invisible
 function OnBecameInvisible ()
 {
     mainScript.soldierVisable = false;
 }
 // ------------------------------------

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

27 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 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

transform.root and empty root game objects don't play nice 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Simple question about OnMouseButtonAsUp 1 Answer

Any problems with this script? 1 Answer

Implementing Setter/Getter 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