• 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 Hamesh81 · May 14, 2013 at 04:52 AM · getcomponentontriggerenteraccessontriggerexitontriggerstay

Can I access a script OnTrigger WITHOUT using getcomponent?

I am trying to optimize one of my scripts by removing getcomponent. Basically I have a trigger, and when certain objects enter it I am accessing that object's script (named "AICar_Script") via getcomponent and changing some variables.

At the moment everything is running fine, but since I have quite a few of these triggers and getcomponent is quite slow I am wondering if it is possible to access the script directly when the object enters the trigger (without using getcomponent each time). The documentation doesn't mention this. Here is the trigger part:

 // Hit the brakes when the AI enters the trigger
 function OnTriggerEnter (other : Collider) {
     if (other.CompareTag ("FastAICar") || ("MediumAICar") || ("SlowAICar")) {
         if (other.gameObject.GetComponent(AICar_Script)) {
             other.gameObject.GetComponent(AICar_Script).Accelerate = false;
         }
     }
 }

I have tried accessing these directly in this way, but I am getting errors on both lines:

 // Hit the brakes when the AI enters the trigger
 function OnTriggerEnter (other : Collider) {
     if (other.CompareTag ("FastAICar") || ("MediumAICar") || ("SlowAICar")) {
         if (other.gameObject.aICar_Script != null) {
             other.gameObject.aICar_Script.Accelerate = false;
         }
     }
 }

In the AICar_Script I have made a reference to itself so that it can be accessed publicly, and I see it getting assigned when I run the the game:

 //Reference to this script so that other scripts can easily access it
 var aICar_Script : AICar_Script;
 
 function Start () {
     
     aICar_Script = GetComponent("AICar_Script");
 }

Am I doing something wrong, or do I simply have to use getcomponent each time when using OnTrigger(whatever)?

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

1 Reply

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

Answer by whydoidoit · May 14, 2013 at 06:32 AM

Well the most optimal way of doing your OnTriggerEnter is:

 // Hit the brakes when the AI enters the trigger
 function OnTriggerEnter (other : Collider) {
     var carAI = other.GetComponent(AICar_Script);
     if(carAI) carAI.Accelerate = false;
 }
Comment
Add comment · Show 5 · 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 whydoidoit · May 14, 2013 at 06:33 AM 0
Share

So no you should use GetComponent, but you should only use it once. And if you are using it like this ignore all of the tag stuff.

avatar image Hamesh81 · May 14, 2013 at 11:50 AM 0
Share

Thank you very much. Can't test this right now, but just of the top of my head, wouldn't that give me a null reference if something without a AICar_Script enters the trigger?

Also just to clarify, if I am using multiple objects that can potentially enter/exit/stay each trigger at the one time, should I use getcomponent only OnTriggerEnter (once), or in OnTriggerStay and Exit as well (three times)?

avatar image whydoidoit · May 14, 2013 at 12:39 PM 1
Share

No it wouldn't:

 if(carAI) ...
avatar image whydoidoit · May 14, 2013 at 01:13 PM 0
Share

You should use it in each function - you have to, that function will be called for each object of which there may well be more than one.

avatar image Hamesh81 · May 14, 2013 at 05:35 PM 0
Share

Ok thanks a lot. I've tested the OnTriggerEnter and Exit so far and it is working well. Stay will take a little more work but I'm sure it'll be fine just the same. Thanks for your help

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

MonoBehaviour.OnTriggerX vs Collider.OnTriggerX 1 Answer

How to make multiple gameobjects instantiate from one collider? 3 Answers

How to use OnTriggerEnter with multiple triggered objects? 1 Answer

OntriggerEnter / Stay with the same Gameobject tags 0 Answers

Problem with OnTriggerEnter 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges