#GetComponent

1 messages · Page 1 of 1 (latest)

cedar warren
#

Is that the script with the error?

frail urchin
#

i jsut have this script?

cedar warren
#

Ok. You said "then it errors". What is the error?

frail urchin
#

when i move the ParticleSystem ps = ObjectToFind.GetComponent<ParticleSystem>(); it errors

#

outside void u

cedar warren
#

where did you move it?

frail urchin
#

start

balmy shoal
#

ps is a local variable in this case.

frail urchin
#

how do i make it not lcoal tho

#

var...

balmy shoal
#

make it a member variable

frail urchin
#

oooh

#

i see

cedar warren
#
private int field;
private void MyMethod()
{
    int localVariable = 0;
}
frail urchin
#

wut

cedar warren
#

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

cedar warren
frail urchin
#

I don't understand

cedar warren
#

Which part?

frail urchin
#

first one

cedar warren
#

copy it and paste it here

frail urchin
cedar warren
#

That's an example

#

of what Praetor explained

frail urchin
#

ooh

cedar warren
#

is it working out for you? @frail urchin

frail urchin
#

no

cedar warren
#

If you explain the issue we'll help you

frail urchin
#
  1. 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.
cedar warren
#

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

frail urchin
#

ok

cedar warren
#

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:

frail urchin
#

Am i doign it wirhgt?

cedar warren
#

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

frail urchin
#

done

cedar warren
#

The script is already on the object yes?

frail urchin
#

yea

cedar warren
#

right-click the script component top pane
select Reset

frail urchin
#

btw

#

it dosen't change if the object has scene as parent of prefab?

#

or does it?

cedar warren
#

Not sure I understand. Could you rephrase that?

frail urchin
#

Like i have the object in a prefab

#

it dosen't not work since i have that or?

cedar warren
#

The one with the script, or with the ParticleSystem?

frail urchin
cedar warren
#

is the script attached to that particle system?

frail urchin
#

no Main

cedar warren
#

ok

frail urchin
#

oke sorry fr interupting

cedar warren
#

no problem

cedar warren
#

Did you see the field get filled with a value?

#

is it now filled*

frail urchin
#

nope i didnt see

cedar warren
#

Are you doing this on the prefab or in the scene?

frail urchin
#

prefab

cedar warren
#

Ok. Drag the object with the Particle System into the field

frail urchin
#

Scene?

cedar warren
#

no

#

sec

frail urchin
#

k

cedar warren
#

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:

frail urchin
#

how i open

#

unpack?

#

or put it in scene

cedar warren
#

When selected from Project view

#

which views the Assets

frail urchin
#

oh yea

#

i have that

cedar warren
#

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.

frail urchin
#

aaah

cedar warren
#

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).

frail urchin
#

so i delete it and only have ps = GameObject.Find("Main").GetComponent<ParticleSystem>(); ?

frail urchin
#

nvm

#

hehe

cedar warren
#

kk

frail urchin
#

don't mind what i say lol

cedar warren
#

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

frail urchin
#

so ps = transform.GetChild(0) ?

cedar warren
#
  • get component
frail urchin
cedar warren
#

does that return an error?

frail urchin
#

nope

cedar warren
#

kk good

frail urchin
#

yes yes

#

I have to go now

#

I have to go to school

cedar warren
#

Ah okay. I want to explain the other approach. You can read it later.

#

Have a nice day o/

frail urchin
#

u too

cedar warren
#

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()

https://docs.unity3d.com/Manual/ExecutionOrder.html

cedar warren
#

@frail urchin Hope this wasn't too much :)
Will be closing the thread by tonight or tomorrow morning.

frail urchin
#

You can close it now

#

I Read through it

cedar warren
#

Alright 👍

frail urchin
#

He

#

Sorry