- Home /
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?
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.
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.
Answer by aldonaletto · Jan 21, 2012 at 12:14 AM
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); }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Audio mixer Setfloat() method problem in coroutine 0 Answers
Storing a method in a variable 2 Answers