- Home /
Get GameObject From Another Script
I am trying to get a Gameobject from another script and assign it to a gameobject in the current script.
Here is the code I have so far:
piece1 = hit.transform.gameObject; //This sets my raycast object as piece1.
piece1.GetComponent<ParticleHolder>();//This gets the script that has the object I want in it.
piece = piece1.particle;//Particle is the name of the object I want to assign to piece.
Answer by amphoterik · Jul 16, 2013 at 03:25 PM
Its your middle line of code that isn't doing anything. I am going to assume that piece and particle are type Particle (since you didn't specify). I would write the code like this:
piece1 = hit.transform.gameObject;
piece = piece1.GetComponent<ParticleHolder>().particle;
Of course, this assumes that particle is public in your ParticleHolder script.
Doesn't this also mean that particle is static? How else would you access the particle variable from the ParticleHolder script.
The second line of code assigns to piece variable an piece1's ParticleHolder scripts public variable particle.
If that would be static, you would get it only this way:
piece = ParticleHolder.particle;