C# scripting with classes/gameobject types

A few words of advice would be of help.

I may be approaching my scripts wrong. I keep wanting to use the script class that’s associated with an object as that object. For example, let’s say I have a board game. I define a C# class called UnitA and associate it with a prefab.

Now let’s say I want to have these objects appear over time. My instinct is to define a method, say UnitA.PlacedOnBoard. So the “launcher” wants to instantiate one or more instances of UnitA, and place each on the board.

I can instantiate a UnitA from a prefab or by cloning one on the board already. These operations take a GameObject or a Transform. But I want to call UnitA.PlacedOnBoard after that. I can’t simple cast to the type UnitA, apparently. What’s the correct mind-set for this type of problem?

Much obliged!

var unitAPrefab : UnitA;

function Start ()
{
  instantiatedUnitA = Instantiate(unitAPrefab);
  uinstantiatedUnitA.PlaceOnBoard(); 
}

The instantiate function can take any kind of object/component/asset.

To get from one component to another (or from a game object to it’s components, use the GetComponent function.

var unitAPrefab : UnitA; 

function Start () { 
  instantiatedUnitA = Instantiate(unitAPrefab); 
  var otherScript : UnitA;
  unitAComponent = instantiatedUnitA.GetComponent(UnitA);
  unitAComponent.PlaceOnBoard();
}

A more losely coupled way is to use SendMessage:

var unitAPrefab : UnitA; 

function Start () { 
  instantiatedUnitA = Instantiate(unitAPrefab); 
  var otherScript : UnitA;
  instantiatedUnitA.SendMessage ("PlaceOnBoard");
}

This way, any (and all) components on the instantiated GO will get a call to PlaeOnBoard…

I think I was running into a chicken-and-egg problem here, because Instantiate needs an existing object. Of course, I could associate a prefab in the Inspector. I guess I was subconsciously avoiding this because if I had a factory class it would need that drag-and-drop association for every type of object it can create. This doesn’t sound too bad, but I prefer to be able to do it all with code (diff’s better, and makes bugs easier to spot.)

I’ve read other suggestions as well. I can see that I was confusing components of a game object (such as the script) with the object itself. This is helpful to know.

I came up with another mechanism to do it in my sleep, basically to have my own classes that represent the logical state of my game world. The game objects could either pull state changes, or the logical representation could control the state of the game objects. Something similar was already discussed in the abstract in these forums before but I now I’m seeing how it can be implemented.

All this information was helpful. Thanks!

Let me verify one thing: is there no way to Instantiate a prefab without having first to manually associate the prefab to a public variable declared in the script?

That is correct. This is in order for Unity to know which assets actually are in use when building, so it won’t include unused assets in the published game.

I could use a little help here.

I’m basically trying to do what is discussed in this thread, BUT do it in C#. ExplosionFromPlayer is a class.

void OnTriggerStay( Collider other ) {
    ExplosionFromPlayer efp = new (ExplosionFromPlayer)other.GetComponent(ExplosionFromPlayer);

I’m getting an “Expecting ;” error on the second line. Really, I thought I’d do it without using “new” making efp a reference to the ExplosionFromPlayer object in “other”… it made more sense to me anyway… yet I got the “got a type expected a value or object” response. I think I might be trying to use this in the wrong way. Essentially, I want to get the value of a variable in this particular collider.

I want to call a function from efp:
if (efp.getIsDamaging()) { //etc.

Am I barking up the wrong tree here or am I just getting something wrong in the syntax?

I think you want:

ExplosionFromPlayer efp = (ExplosionFromPlayer)other.GetComponent(typeof(ExplosionFromPlayer));

Tricky verbose C# syntax.

That’s exactly what I want and it works like I want it too, as well. Nice. :slight_smile: Thanks.

I’m generally enjoying C#, but it does get a tad verbose and little things like this are a showstopper from time to time. Having this work is opening up a bunch of possibilities that I’ve been avoiding so far, though.

Thanks, again. I’m getting neck deep in my project now, so the quick reply is much appreciated. I might even try to enter it in OMG. 8)