• 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
Question by Hamesh81 · May 19, 2013 at 02:17 PM · getcomponentreferencereferences

Can I access a script referenced in another script (without getcomponent)?

I am trying to limit the amount of "getcomponent" calls I use.

I have 4 scripts that need to talk to each other. First is a weapon script (called firearm) which simply fires the next available projectile, second is projectileList which manages all of the projectiles in the scene via a generic list and assigns one to a variable (called currentProjectile) ready to be fired by the weapon script. Next is the script on the actual projectiles themselves, projectileDecals, this references all the necessary child scripts one of which is a standard particleEmitter (the fourth script).

From the firearm script I need to be able to access not only the projectile being fired (which I already can) but also its child scripts like the particleEmitter. But when from the firearm script I try to access the reference I made in the projectile (projectileDecals script) to its particleEmitter for example it does not work. This is strange because I can access components such as the projectiles transform, rigidbody etc the same way, just not the referenced particleEmitter (see the FireOneShot function below). Do I need to make a new direct reference to it in firearm script itself? Because that would mean I would have to use getcomponent each time for every new projectile which I am trying to avoid.

Comment

People who like this

0 Show 2
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 Fattie · May 19, 2013 at 02:27 PM 0
Share

"I am trying to limit the amount of "getcomponent" calls I use."

Why? You only use those at start up time (basically .. in Awake() )

Do not use them otherwise. Use them in Awake() and get a variable, then your'e all set for the rest of the game.

avatar image Hamesh81 · May 19, 2013 at 02:44 PM 0
Share

Yes I only use them during start right now, that's my point. I don't want to have to use getcomponent during update whenever a new projectile is assigned. My script works similarly to a pooling system, where I search through all the available projectiles and fire the one available. This means I can't reference the current projectile during start/awake because at runtime this will be constantly changing.

5 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Fattie · May 19, 2013 at 02:32 PM

it's no problem at all to get one from another,

for example, in GrandMaster

 @System.NonSerialized
 var brain:MyAIRoutine;
 @System.NonSerialized
 var size:float;

You would find "brain" in the normal way using something like ..

 brain = GameObject.Find(" something ").GetComponent(MyAIRoutine);

then in another script, you can (if you want) simply get -- or indeed, just use -- "brain"

So in ScriptBB

 @System.NonSerialized
 var myBoss:GrandMaster;

So, you establish "myBoss" using find, etc.

then, anywhere you want in ScriptBB, you can just refer to myBoss.brain

that will take you "on to" the "MyAIRoutine" system.

  myBoss.brain.explode();
  myBoss.brain.limits = 1.32;

recall that similarly you can use "ordinary" variables in myBoss

  myBoss.size = 12inches;

no problem, right?

Comment
Hamesh81

People who like this

1 Show 14 · 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 Hamesh81 · May 19, 2013 at 02:50 PM 0
Share

I thought this was exactly the case, why is it though that I can't access the particleEmitter but have no trouble getting the transform, rigidbody components etc!? I have spent the last two days staring at that one line in my script (function FireOneShot), and trying different ways to access it. Can you see any mistakes at all in the code I posted above, because I really don't know where I'm making the mistake.

avatar image Fattie · May 19, 2013 at 03:05 PM 0
Share

you forgot

@System.NonSerialized

always use it. never, ever use the "editor inspector setting" thing in Unity

avatar image Hamesh81 · May 19, 2013 at 03:31 PM 0
Share

Sorry what do you mean by "editor inspector setting", dragging and dropping for public variables? I also tried @System.NonSerialized but it only unassigned all of my public variables. I personally prefer dragging and dropping to using GameObject.Find, I thought it was better for performance reasons since GameObject.Find is quite slow. Did you find anything else wrong with my scripts apart from that? I've thoroughly checked all the script references and I can see all of them getting assigned at runtime. Appreciate your time and help so far.

avatar image Fattie · May 19, 2013 at 03:36 PM 0
Share

"I personally prefer dragging and dropping"

Fine --no problem

could you just say WHAT IS NOT WORKING, which one variable is not working, me or someone will try to spot the woe.

avatar image Hamesh81 · May 19, 2013 at 04:00 PM 0
Share

Sure, in the firearm script, function FireOneShot, you will see that I access the transform and rigidbody of the currentProjectile (this has the script projectileDecals attached directly to it) which is a public gameobject assigned in the projectileList script. Accessing these components works without issues, but when I try to access the particle emitter component referenced as trailParticleEmitter (in projectileDecals) it cannot find it. That is the problem.

Show more comments
avatar image

Answer by Eric5h5 · May 19, 2013 at 08:22 PM

Using GetComponent is not a big deal. Don't make your code a mess trying to avoid it; it's not even slightly worth it. Just don't use strings.

 // Wrong:
 projectileList = projectileManager.GetComponent("ProjectileList"); 

 // Right:
 projectileList = projectileManager.GetComponent(ProjectileList);
Comment
Hamesh81

People who like this

1 Show 11 · 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 Fattie · May 19, 2013 at 08:38 PM 0
Share

yes, yes, yes .. exactly. Well said.

avatar image Hamesh81 · May 20, 2013 at 05:41 AM 0
Share

What about in the case of firing projectiles/bullets? I'll be firing them at up to 1200 rounds per minute (20 times per second) which would mean using getcomponent for each round. Isn't that unnecessarily heavy?

Since I'm not instantiating but rather reusing a pool of projectiles, I thought I would save some runtime computing by referencing the components before hand.

avatar image Eric5h5 · May 20, 2013 at 05:56 AM 0
Share

As I said, GetComponent really is not a big deal, but it's highly unlikely you want to actually be using so many GameObjects, even if you are pooling them. GetComponent is the least of your worries. It's much more common to do raycasting and basically "pretend" to be firing bullets. Games are all smoke and mirrors.

avatar image Fattie · May 20, 2013 at 05:58 AM 0
Share

Well you have to do that with a pool .. you just have a number of Lists tht point to all the different aspects of the bullets.

so you'd have a List that points directly to the transform on each one, another List that points directly to the SmashManager script on each one, another List that points directly to the collider on each one, and so on.

Say you're programming along and you find, oh, I need to use the -whatever- on the bullets, just go back to the pool script and add another List that keeps track of all of that item on the bullets.

So maybe that's what you're asking here? You just have a separate List (NOTE - DON'T USE ARRAY) for each and every thing you use on the bullet pool.

I've explained this unbelievably boring and singularly uninteresting topic at length here, it includes full code examples ...

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

Please vote that answer up as it's annoyingly stuck on 25 and I need MOAR POINTS MAN

avatar image Fattie · May 20, 2013 at 08:45 AM 1
Share

now you're having high-level meetings on projectile strategy dude, and you can't get freaking getComponent working :)

Show more comments
avatar image

Answer by MaDDoX · May 20, 2013 at 06:16 AM

Maybe a silly question, but have you tried: trail.gameObject.GetComponent(ParticleEmitter); ?

Comment

People who like this

0 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 Hamesh81 · May 20, 2013 at 08:43 AM 0
Share

No not yet, because I'm only accessing a reference to the particle emitter. The script where I actually use getcomponent to make the reference is assigning the script fine at runtime, I can check because the variable is public.

avatar image

Answer by TheRichardGamer · May 29, 2013 at 04:50 PM

You can try this:

var myScript : monobehaviour;

//Down below shows on how you can access the script's variables.

myScript.myVariable = true;

Comment

People who like this

0 Show 0 · 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

Answer by Mill0698 · May 29, 2013 at 05:38 PM

Perhaps create a public static variable? maybe i read you question wrong

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Reference Not Set toInstance of Object 1 Answer

Script Referencing 2 Answers

.getComponent() creates an Instance? 1 Answer

the most RESOURCE EFFICIENT way of referencing 1 Answer

How to change a value of a reference object variable after a while ? 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