1

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 !

3
  • In the "another script", are you sure the planet is already instantiated before calling GetComponent ? Nothing wrong in your code, probably a script loading order problem. Commented Mar 8, 2017 at 13:58
  • Yes, in the another script, The GameObject "this" is the GameObject clicked in my scene, so he is real :) A script loading order problem ? Commented Mar 8, 2017 at 13:59
  • waitwaitwait. Something strange with what you think "this" is Commented Mar 8, 2017 at 14:02

2 Answers 2

4

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;
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Does the GameObject.Find("someStringHere") find an object by it's name in the editor? And what happens if there are multiple entities with the same name?
"Does the GameObject.Find("someStringHere") find an object by it's name in the editor?" Yes. "And what happens if there are multiple entities with the same name?" It finds the first one only. If you plan to use it make sure that your object names are unique. If two objects have the-same name but under different objects then you can use "/" to find it as a folder name which is cool. For example, two Door Objects under different Objects: GameObject.Find("House/Door"); and GameObject.Find("Car/Door");
I think I have another thing, I'm working on a Clone on the real GameObject Planet. Is this a problem ? Do the clone either copy the instance of the component Planet ?
Planet clonePlanet = GameObject.Instantiate(My_Original_planet, GameObject.Find("SolarSystem").transform);
Yeah, I know now it's not a good solution :) Thanks for help I will think about that ;)
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.