- Home /
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.
"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.
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. $$anonymous$$y 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.
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?
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.
you forgot
@System.NonSerialized
always use it. never, ever use the "editor inspector setting" thing in Unity
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.
"I personally prefer dragging and dropping"
Fine --no problem
could you just say WHAT IS NOT WOR$$anonymous$$ING, which one variable is not working, me or someone will try to spot the woe.
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.
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);
What about in the case of firing projectiles/bullets? I'll be firing them at up to 1200 rounds per $$anonymous$$ute (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.
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.
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 Smash$$anonymous$$anager script on each one, another List that points directly to the collider on each one, and so on.
Say you're program$$anonymous$$g 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 $$anonymous$$OAR POINTS $$anonymous$$AN
now you're having high-level meetings on projectile strategy dude, and you can't get freaking getComponent working :)
Answer by MaDDoX · May 20, 2013 at 06:16 AM
Maybe a silly question, but have you tried: trail.gameObject.GetComponent(ParticleEmitter); ?
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.
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;
Answer by Mill0698 · May 29, 2013 at 05:38 PM
Perhaps create a public static variable? maybe i read you question wrong
Your answer
Follow this Question
Related Questions
Script Referencing 2 Answers
Reference Not Set toInstance of Object 1 Answer
get same var from multiple different scripts 1 Answer
Why can't I change another script's variable with this script? Thanks, 1 Answer
Learn Scripting Easy? 5 Answers