Variable name to get Component

Hello

I try to make my object system, I will talk about it, and I would your opignon about it.

I have currently three objects model: Cube and Capsule, and each have their script (CubeController ; CapsuleController). And when I get my object, I need his name so I make a other script ObjectName with public string Name, and I get this name.

string objectName = Object.GetComponent().Value;

But after, I need get my Controller for my Cube/Capsule.
How do that? Because of course my code doesn’t work

objectName+“Controller” ObjectTmp = Object.GetComponent(objectName+“Controller”);

Other question, for example each cube can contain 5 objects, how can I save the instantiate object (XController) because I need to keep those proprieties. Create a Array[5] of GameObject and SetActive to False ?

Thank you

You don’t need to get the component to get the name or to set active on it, you can do that on the game object. If you need to call Methods on various classes that might be different, you can use interfaces :
https://unity3d.com/learn/tutorials/modules/intermediate/scripting/interfaces

1 Like

I don’t really understand, and my code doesn’t work (it seems same in the tutorial but…)

http://pastebin.com/RNLe3T0X

If you can show me with little example…
Thank you

Your question doesn’t make a lot of sense. Perhaps some time in the learn section will help? Roll-A-Ball is a good start. As is the rest of the scripting section.

I also saw ROLL-A-BALL, and it’s not want I want to do.

Why my question doesn’t make lot of sense ?

Because there are so many basic things you need to understand to approach this question. It strikes me that there are underlying structural issues to how you are building your code.

The interface answer is probably the best mechanical solution. There is a good tutorial on interfaces in the learn section. Or you could use reflection for a true string based component selection.

But both answers miss the point of why you are doing this and should you be doing it.

I’m trying to make a object system, and I watched the tutorials video on Interface, and reproduce it but I got an error.

Code and error details?

If you post your code here, people will see your code and can help you directly.

using UnityEngine;

public interface ObjectController
{
    float Health();
}
using UnityEngine;

// Assets/Scripts/Objects/CubeController.cs(3,14): error CS0535: `CubeController' does not implement interface member `ObjectController.Health()'
public class CubeController : MonoBehaviour, ObjectController
{
    private string Name = "Cube";
    public float health;

    void Start()
    {
        health = 100f;
    }

    public float Health
    {
        get
        {
            return health;
        }
        set
        {
            health = value;
        }
    }
}

Here a quick explanation of the issue. In ObjectController, you define that Health is a method that returns a float. But in CubeController, you don’t implement such a method, leading to the compilation error. Instead, you implement health to be a property with a getter and setter.

You find the correct way to define properties in interfaces here:

Edit: Split up the code into two parts.

Oh yeah, thank it works… but I think interface is not really want I want.

For exemple, if I want Health only for my CapsuleController? And I want a Arraw of Object for my Cube (cube can contain 5 other objets for example)

You understood my problems ?