Change instance of a Custom Class from a another script

Hello,

I create a game with planets and a player.
On the first hand I have into planets:

  • Custom Class GravityPlanetAttractor and a scipt swithPlayerPlanet.
public class GravityPlanetAttractor : MonoBehaviour
{
      ...;
}

And on the other hand my Player with:

  • A script witch control mouvement and rotation and a script for attract a rotate this around the current planet
public class GravityBodyScript : MonoBehaviour
{
    //objet attractif
    public GravityPlanetAttractor planet;

...;

}

I would like to change the instance of a planet when the player enter in a new planet.

For that I have a scipt witch use a instance of GravityPlanetAttractor per planets.
I would like, with the next script, change the reference of the GravityPlanetAttractor to the new planet witch the player is enter.

private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            GravityPlanetAttractor planet = transform;
            gameObject.GetComponent<GravityBodyScript>().planet = planet;
        }
    }

but obviously unity don’t create a method to change a transform object into a GravityPlanetAttractor, so I don’t know how to do this.

Can any one (understand me and ) help me ?

thanks for reading and for answers.

You don’t change the transform component, you use GetComponent, which you’re already using in the same method. I’m having a hard time reading what you’ve written, so not exactly sure what object the GravityPlanetAttractor is supposed to be attached to. Whichever object it is attached to is the object you use GetComponent on.

First of all thaks for answer.

I know I don’t have to use the transform component, As I don’t know what write here I write this to draw my Idea.

Ok thanks I didn’t realize I have to use still the same GetComponent.

The last part of code (OnTriggerEnter(){ … }) is attached planets.

Ok after some change I find how to fix my variable ^^.

I write this:

private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            GravityPlanetAttractor planetAttractor = gameObject.GetComponent<GravityPlanetAttractor>();
            player.GetComponent<GravityBodyScript>().planet = planetAttractor;
        }
    }