0
\$\begingroup\$

I have a custom class named Stars:

class Star
{
    public int primaryID;
    public string properName;
    public string HIPID;
    public string HDID;
    public string HRID; 
    public string GLID;
    public string BFID;
    public decimal rightAscension;
    public decimal declination;
    public string magnitude;
    public string colourIndex;
    public int scale;
    public int red;
    public int green;
    public int blue;
    public bool isSimulated;
    public double x;
    public double y;
    public double z;

    public void setDetails(int primaryID, string properName, string HIPID, string HDID, string HRID, string GLID, string BFID, decimal rightAscension, decimal declination, string magnitude, string colourIndex)
    {
        this.primaryID = primaryID;
        this.properName = properName;
        this.HIPID = HIPID;
        this.HDID = HDID;
        this.HRID = HRID;
        this.GLID = GLID;
        this.BFID = BFID;
        this.rightAscension = rightAscension;
        this.declination = declination;
        this.magnitude = magnitude;
        this.colourIndex = colourIndex;
        decimal deg2rad = 0.01745329251994329576923690768489m;

        this.isSimulated = true;

        // Decimal datatype is used to make sure that values are very accurate and precise
        decimal degrees_ra;
        decimal degrees_dec;
        decimal radians_ra;
        decimal radians_dec;

        degrees_ra = Decimal.Multiply(rightAscension, 15);
        degrees_dec = declination;

        radians_ra = Decimal.Multiply(degrees_ra, deg2rad);
        radians_dec = Decimal.Multiply(degrees_dec, deg2rad);

        // Converted from decimal to double because the Math library cannot work with decimals
        float distanceStars = 19113;

        this.x = (distanceStars * Math.Cos(decimal.ToDouble(radians_dec)) * Math.Cos(decimal.ToDouble(radians_ra)));
        this.y = (distanceStars * Math.Cos(decimal.ToDouble(radians_dec)) * Math.Sin(decimal.ToDouble(radians_ra)));
        this.z = (distanceStars * Math.Sin(decimal.ToDouble(radians_dec)));
    }

    public Vector3 getVector()
    {
        return new Vector3((float)z, (float)y, (float)x);
    }
}

There are many of these stars and I want to instantiate them at run-time:

void plotStars()
{
    int i = 0;
    while (i < (StarDataBank.Instance.NumOfStars))
    {
        string name = StarDataBank.Instance.StarName[i];
        string HIP = StarDataBank.Instance.StarIDHIP[i];
        string HD = StarDataBank.Instance.StarIDHD[i];
        string HR = StarDataBank.Instance.StarIDHR[i];
        string GL = StarDataBank.Instance.StarIDGL[i];
        string BF = StarDataBank.Instance.StarIDBF[i];
        decimal RA = Convert.ToDecimal(StarDataBank.Instance.StarRA[i]);
        decimal Dec = Convert.ToDecimal(StarDataBank.Instance.StarDec[i]);
        string Mag = StarDataBank.Instance.StarMag[i];
        string ColI = StarDataBank.Instance.StarCI[i];

        var star = new Star();
        star = Instantiate(ObjectStar, transform.position + star.getVector(), Quaternion.identity);

        star.transform.parent = StarObject.transform;
        star.transform.localScale = new Vector3(star.scale, star.scale, star.scale);
        i++;
    }
}

But I am experiencing a lot of errors.

  1. Unity isn't instantiating the Stars because it can't convert UnityEngine.Transform to Star
  2. Star doesn't contain definition of Transform

Is there a way to assign a class as a gameobject?

\$\endgroup\$
  • \$\begingroup\$ Did you mean to make Star a component you can attach to a GameObject by inheriting from MonoBehaviour? How is your ObjectStar variable defined and initialized? \$\endgroup\$ – DMGregory Feb 21 at 15:07
  • \$\begingroup\$ ObjectStar is declared as public Transform ObjectStar; And a prefab has been attached to this through the unity editor \$\endgroup\$ – SidS Feb 21 at 15:25
  • \$\begingroup\$ How do I attach this class as a gameobject? and will that allow me to instantiate it? \$\endgroup\$ – SidS Feb 21 at 15:26
1
\$\begingroup\$

It looks like you're using the variable named star to do double-duty:

var star = new Star();

This says "make a new data structure to hold information about this star"

star = Instantiate(ObjectStar, ...);

This says "make a new copy of the ObjectStar game object to represent this star in the game scene, and give me its Transform (since the ObjectStar variable is of type Transform"

These two uses are contradictory. This variable cannot simultaneously hold a Star and a Transform. Pick one or the other.

So, use separate variables for these two uses:

Star star = new Star();
Transform sceneObject = Instantiate(ObjectStar, ...);`

If you want to associate information from this star data structure with your scene object, you have a few options:

  • You can use a Dictionary to store a mapping from star data to scene objects, or vice versa

  • You can make your Star class a MonoBehaviour component that can be attached to a game object, just like other components in Unity, using AddComponent in script, or the button at the bottom of the Inspector.

  • You can make a new MonoBehaviour component to attach to your scene object, that holds a reference to a Star data structure. This lets you get the advantages of bolting the component directly to the object it relates to, while keeping your Star class uncluttered with MonoBehaviour stuff — in case your Star class needs to be serialized or inter-operate with non-Unity code elsewhere in your project.

Here's an example of that last style:

public class StarRepresentation : MonoBehaviour {
    public Star starData;
}

You can set up your ObjectStar object to have one of these StarRepresentation components attached, by using the "Add Component" button in the Inspector.

Then you can change your declaration of the object to...

public StarRepresentation starPrefab;

Making it use the StarRepresentation type means that when you Instantiate it, you'll get the component you want directly, without needing a GetComponent call.

Now when you want to instantiate & populate your new star object with star data, you can do it like this:

StarRepresentation representation = Instantiate(starPrefab, ...);
representation.starData = new Star ();

Then you can fill in the details of this starData as you like.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.