简体   繁体   中英

Unity instance not saved between scripts

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 !

this.gameObject.GetComponent<Planet>() and this.GetComponent<Planet>() are both the-same thing since they will both try to get the Planet component from the this GameObject this script is attached to.

The thing to look at for here is this . You are referring to this script. When you do that from another script, you are trying to the Planet component from this script that is executing the GetComponent function. If this script does not have the Planet component, you get null . I am sure this is what's happening.

I would suggest naming the GameObject you just created:

GameObject planet2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
planet2.AddComponent<Planet>();
//Name it
planet2.name = "Your Other GameObject";

Now, find the GameObject from another script then access the Planet component from it:

GameObject obj = GameObject.Find("Your Other GameObject");
//Now you can do 
Planet planetCompo2 = obj.GetComponent<Planet>();

Since you already have the variable Name in your Planet script, you can all Planet script with GetComponent[s] then just loop through it and check which one has the name " Planet 2 ".

Planet planetCompo2 = null;

//Get all Planet components
Planet[] tempComPlanet = GetComponents<Planet>();

//Seatch for which one has the name "Planet 2"
for (int i = 0; i < tempComPlanet.Length; i++)
{
    if (tempComPlanet[i].Name == "Planet 2")
    {
        //Found. Store it to planetCompo2 then exit the loop
        planetCompo2 = tempComPlanet[i];
        break;
    }
}

Using the keyword 'this' in your other script will reference that object looking for a Planet script attached to it. You should either look for the Game Object that has the script attached or instantiate one.

Apply a tag like PlanetObject to your game object with the planet script and use something like the following:

var planet = GameObject.FindWithTag("PlanetObject").GetComponent<Planet>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM