x


Setting variable when method called

private void EnemySpawn(){
       enemy = new GameObject(enemyName);
       arms = new GameObject ("arms");
       arms.AddComponent("PlayerDamage");
       arms.AddComponent("BoxCollider");
       enemy.AddComponent("MeshFilter");
       enemy.AddComponent("MeshRenderer");
       enemy.AddComponent("BoxCollider");
       enemy.AddComponent("EnemyFollow");
       enemy.AddComponent("EnemyHealth");
       arms.GetComponent<BoxCollider>().isTrigger = true;
       arms.GetComponent<BoxCollider>().size = new Vector3 (armsColliderSizex, armsColliderSizey, armsColliderSizez);
       enemy.GetComponent<MeshFilter>().mesh = bodyMesh;
       enemy.GetComponent<MeshRenderer>().material.mainTexture = bodyMaterial;
       enemy.GetComponent<BoxCollider>().size = new Vector3 (bodyColliderSizex, bodyColliderSizey, bodyColliderSizez); 
       enemy.GetComponent<BoxCollider>().center = new Vector3 (0, bodyColliderCentery, bodyColliderCenterz);
       enemy.GetComponent<BoxCollider>().isTrigger = true;
       enemy.transform.position = new Vector3(bodyTransformPosx, bodyTransformPosy, bodyTransformPosz);
       arms.transform.parent = enemy.transform;
       arms.transform.position = new Vector3(bodyTransformPosx, armsTransformPosy, armsTransformPosz);
    }

I basically want to just put EnemySpawn(x,y,z) in void Start instead of manually creating every single one and setting the coordinates within the method. Is this possible?

more ▼

asked Jan 20 '12 at 11:06 PM

ohwhynot gravatar image

ohwhynot
1 1 2

That should work.

Jan 20 '12 at 11:16 PM jahroy

It's really not clear what you are trying to do. What is stopping you from calling EnemySpawn(Vector3 Position) in Start()? For something this complex (an object with several scripts attached), it might be advisable to create a Prefab and Instantiate it. But if you want to do it programmatically, you seem to be on the right track.

Jan 20 '12 at 11:18 PM Thom Denick

That's what I'm having trouble with. If it wouldn't be too convenient, could you elaborate? I'm new to this kind of stuff.

Jan 21 '12 at 12:13 AM ohwhynot
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can do it more easily defining the arms offset relative to the body in a constant variable and assigning it to arms.localPosition, and assigning the passed coordinates to the enemy transform.position:

public Vector3 armsOffset = new Vector3(0, 1.1, 0.5); // relative arms position

private void EnemySpawn(float x, float y, float z){
       enemy = new GameObject(enemyName);
       arms = new GameObject ("arms");
       ...
       enemy.transform.position = new Vector3(x, y, z); // assign the body position
       arms.transform.parent = enemy.transform;
       arms.transform.localPosition = armsOffset; // set the relative arms position
}

But the best solution by far is to create your enemy in the Editor, then make it a prefab: create the empty object and attach the scripts, components, meshes, colliders etc. Test it, do any fine tuning you want and, when everything is ok, drag it to the Project View to make it a prefab. Using a prefab, your EnemySpawn function would be reduced to this:

GameObject enemyPrefab; // set this variable in the Inspector

private void EnemySpawn(float x, float y, float z){
    Instantiate(enemyPrefab, new Vector3(x, y, z), Quaternion.identity);
}
more ▼

answered Jan 21 '12 at 12:14 AM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4154
x55

asked: Jan 20 '12 at 11:06 PM

Seen: 482 times

Last Updated: Jan 21 '12 at 12:19 AM