• 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 /
  • Help Room /
avatar image
0
Question by manaslegodesigns · Dec 04, 2017 at 11:16 AM · instantiatedestroy object

Enemy spawning problem

i have an enemy which when crosses a point on the x i destroy it. This is the script: if(enemy.transform.position.x<=-4.7){ Destroy(enemy); DestructiveScript.des -= 1; }

Also when the enemy crosses a point i instantiate it. This is the script:

if (enemy.transform.position.x >= st && enemy.transform.position.x <= en && and) { Instantiate(enemy); DestructiveScript.des += 1; }

When there are no enemies in the game i want to instantiate a enemy. This is the script:

public GameObject enemy; public static int des = 1; void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     if(des==0){
         Instantiate(enemy);
     }
 }


Problem is that when the last enemy is so called destroyed , it generates the enemy but generates it just where the previous enemy died even though i have a script that should spawn it somewhere else.

Here is that script:

 public class enemyscript : MonoBehaviour {
 public GameObject enemy;
 public Text score;
 int abc=1;
 public float speed;
 private Vector3 one = new Vector3(4.5f, 0.6f, 0f);
 private Vector3 two = new Vector3(4.5f, 0.6f, 1f);
 private Vector3 three = new Vector3(4.5f, 0.6f, 2f);
 private Vector3 four = new Vector3(4.5f, 0.6f, -1f);
 private Vector3 five = new Vector3(4.5f, 0.6f, -2f);
 public Vector3 store;
 public float st;
 public float en;
 private int i = 0;
 bool xc = true;
 Vector3 forwardRotationPoint;
 Vector3 backRotationPoint;
 Vector3 leftRotationPoint;
 Vector3 rightRotationPoint;
 Bounds bounds;
 bool rolling;
 bool and = true;
 int a;
 int lolipop;
 private long dodgefrog;
 double valuOfScore = 0;

 bool isThere = false;
 void Start()
 {
     lolipop = 1;
     dodgefrog = 0;
     store = Gen(store);
     enemy.transform.position = store;
     bounds = GetComponent<MeshRenderer>().bounds;
     forwardRotationPoint = new Vector3(0, -bounds.extents.y, bounds.extents.z);
     backRotationPoint = new Vector3(0, -bounds.extents.y, -bounds.extents.z);
     leftRotationPoint = new Vector3(-bounds.extents.x, -bounds.extents.y, 0);
     rightRotationPoint = new Vector3(bounds.extents.x, -bounds.extents.y, 0);

 }
 private void OnCollisionEnter(Collision collision)
 {
     if(collision.gameObject.name.Equals("playerdup")){
         Time.timeScale = 0;


     }
 }
 void Update()

 {
    
     
     
     if(enemy.transform.position.x<=-4.7){
         Destroy(enemy);
         scorecontroller.scorevalue += 1;
         DestructiveScript.des -= 1;
         lolipop-=1;
         isThere = true;



     }



     
     //if (iii == 0&&isThere)
     //{
     //    iii++;
     //    Instantiate(enemy);
     //    Debug.Log("Extra" + iii);

     //}





     if (enemy.transform.position.x >= st && enemy.transform.position.x <= en && and)
     {
         Instantiate(enemy);
         lolipop += 1;
         DestructiveScript.des += 1;

         and = false;
     }

     if(lolipop==0){
         Instantiate(enemy);

     }

     if (rolling)
         return;



     StartCoroutine(Roll(leftRotationPoint));

 }

 private IEnumerator Roll(Vector3 rotationPoint)
 {
     Vector3 point = transform.position + rotationPoint;
     Vector3 axis = Vector3.Cross(Vector3.up, rotationPoint).normalized;
     float angle = 90;
     float a = 0;

     rolling = true;
     and = true;
     while (angle > 0)
     {
         a = Time.deltaTime * speed;
         transform.RotateAround(point, axis, a);
         angle -= a;
         yield return null;
     }

     transform.RotateAround(point, axis, angle);

     rolling = false;




 }
 public Vector3 Gen(Vector3 sam)
 {
     int posStart = Random.Range(0, 5);
     if (posStart == 0)
     {

         sam = one;
     }
     else if (posStart == 1)
     {

         sam = two;
     }
     else if (posStart == 2)
     {

         sam = three;
     }
     else if (posStart == 3)
     {
         sam = four;
     }
     else if (posStart == 4)
     {
         sam = five;
     }
     return sam;
 }


Any help would definitely be appreciated Thanks

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

136 People are following this question.

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

Related Questions

instantiate and delete trouble with my NPC 0 Answers

How do I destroy an instantiated game object when player leaves trigger area? 1 Answer

How to destroy an object clone? 0 Answers

Third person controller moving backwards when shooting 0 Answers

Destroy specific clone 3 Answers


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