I have a script Planet.cs like :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Planet : MonoBehaviour
{
public string Name { get; set; }
}
In a custom function I need to generate multiple planets, so I create a new GameObject, I add the component Planet (my script), and I set My attribute like :
GameObject planet2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
planet2.AddComponent<Planet>();
Planet planetCompo2 = planet2.GetComponent<Planet>();
planetCompo2.Name = "Planet 2";
If I check in the end of my function, all attributes are setted, If I check the attribute Name of any planets it's ok.
My problem is, in another script, I try to get a Planet from a GameObject with component Planet, but I always have a null Object.
Example : this.gameObject.GetComponent<Planet>() or this.GetComponent<Planet>() are always null where this is the GameObject with Planet Component.
Why my instances are not saved ? What my error ? Thanks !