#GetComponent
1 messages · Page 1 of 1 (latest)
i jsut have this script?
Ok. You said "then it errors". What is the error?
when i move the ParticleSystem ps = ObjectToFind.GetComponent<ParticleSystem>(); it errors
outside void u
where did you move it?
start
Local variables can't be accessed across methods
ps is a local variable in this case.
make it a member variable
private int field;
private void MyMethod()
{
int localVariable = 0;
}
wut
There are a few ways to reference the component.
Either it is a private field, and GetComponent is used in Awake(), which runs before anything else
Or a [SerializeField] private field, that is exposed in the inspector. (like public)
For that, we use void Reset() instead of Awake()
More is explained here:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
What is confusing?
I don't understand
Which part?
first one
copy it and paste it here
ooh
is it working out for you? @frail urchin
no
If you explain the issue we'll help you
- I have no idea what u meant. 2. I tried a new thingy and it still do the same. 3. Im about to recode the whole script. 4. Im about to give up.
Well, after we gave instructions, we expected to hear how it went, or the next question on your mind 😅 No need to give up.
We'll make this one simple.
Delete everything inside the class brackets
ok
Then write [SerializeField] private ParticleSystem ps;
private variables are only accessible to that particular instance (copy) of the script.
a MonoBehaviour script can be placed on multiple objects, and behave independently
[SerializeField] exposes the private variable to the inspector
exactly like public does, except public variables are also accessible from other scripts
Now make a private void Reset() method
inside it, place the following code:
Am i doign it wirhgt?
It needs the curly-brackets to have a body
private void Reset()
{
}
inside it
ps = GameObject.Find("name").GetComponent<ParticleSystem>();
Make sure the name is right
Then Save the script
done
The script is already on the object yes?
yea
right-click the script component top pane
select Reset
Not sure I understand. Could you rephrase that?
The one with the script, or with the ParticleSystem?
is the script attached to that particle system?
no Main
ok
oke sorry fr interupting
no problem
done
nope i didnt see
Are you doing this on the prefab or in the scene?
prefab
Ok. Drag the object with the Particle System into the field
Scene?
Yes!
I just tested myself to make sure I wasn't missing anything.
You need to have the prefab opened, not just selected from Project Assets
So, two things:
Assets can be added to scenes. That creates an instance of that asset (or prefab).
So first you configure the prefab, and then you add it to the scene, and it should work out of the box, or with little extra work
in our case
You don't need GameObject.Find
because transform.GetChild(0)
or transform.Find("childname")
would do the work for less resources.
aaah
Either way. You don't need to use void Reset() if you are comfortable with drag-n-dropping your serialized references.
It's just useful for default values, and GameObject.Find() won't work in a build anyway, which is why I only use it in void Reset (which is editor-only and stripped from build).
so i delete it and only have ps = GameObject.Find("Main").GetComponent<ParticleSystem>(); ?
uh, what did you delete?
kk
don't mind what i say lol
np
just change GameObjet.Find("whatever")
with transform.GetChild(0).
If you add more child objects, just make sure the particle system remains the first child (visually) so it is always index 0
so ps = transform.GetChild(0) ?
- get component
does that return an error?
nope
kk good
Ah okay. I want to explain the other approach. You can read it later.
Have a nice day o/
u too
Having the reference serialized like this, before runtime, is the most efficient.
Prefabs can store references to themselves, but obviously not to anything in a scene, until you have a copy of a prefab inside a scene, and then you can reference an object there.
The other way is running that same code inside void Awake(), which will run before anything else in MonoBehaviour.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
If the reference is to a different object, you need to wait until all the other objects have actually awoken, so those references must be fetched in Start()
@frail urchin Hope this wasn't too much :)
Will be closing the thread by tonight or tomorrow morning.
Alright 👍