Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ohwhynot · Jan 20, 2012 at 11:06 PM · c#method

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · Jan 20, 2012 at 11:16 PM 0
Share

That should work.

avatar image Thom Denick · Jan 20, 2012 at 11:18 PM 0
Share

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.

avatar image ohwhynot · Jan 21, 2012 at 12:13 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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); }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

passing an arraylist to a separate script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges