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 rinwatergirl · Mar 22, 2012 at 04:42 PM · newbiedestroy object

Help with "Kaboom" game programming, Unity first time user.

Okay so I'm making a game that is like Kaboom, and I'm having a problem with making an instance of my bomb object appear.

Here's the code for the bomb object it self:

using UnityEngine; using System.Collections;

public class Bomb : MonoBehaviour { public int moveSpeed = 60; Vector3 moveDirection = new Vector3(0,-1,0);

 // Use this for initialization
 void Start () 
 {
 
 }
 
 // Update is called once per frame
 void Update () 
 {
     Vector3 newPosition = moveDirection * (moveSpeed * Time.deltaTime);
     transform.position = transform.position + newPosition;
     
     if(newPosition.y > -40)
     {
         Destroy(transform.gameObject);
     }
 }
 
 void OnCollision(Collision collision)
 {
     Destroy(transform.gameObject);
 }

}

And I'm trying to get the enemy object to create a new instance of the bomb, here's the enemy object's code:

using UnityEngine; using System.Collections;

public class Enemy : MonoBehaviour { public int moveSpeed = 40; public bool isComputer = false; Vector3 computerDirection = Vector3.right; public bool onScreen = true; public Transform bomb;

 // Use this for initialization
 void Start () 
 {
 
 }
 
 // Update is called once per frame
 void Update () 
 {
     Vector3 newPosition = Vector3.zero;
     newPosition = computerDirection * (moveSpeed * Time.deltaTime);
     
     newPosition = newPosition + transform.position;
     
     if(newPosition.x > 20)
     {
         newPosition.x = 20;
         computerDirection.x *= -1;
     }
     
     else if(newPosition.x < -20)
     {
         newPosition.x = -20;
         computerDirection.x *= -1;
     }
     
     transform.position = newPosition;
     
     if(onScreen == true)
     {
         Transform newBomb = Instantiate(bomb,transform.position, transform.rotation) as Transform;
     }
 }

}

The thing that won't work is when I'm trying to create a new instance of the bomb while the enemy object is on screen, after the original bomb object is destroyed.

Any ideas??

Comment
Add comment · Show 2
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 Meltdown · Mar 22, 2012 at 06:37 PM 0
Share

How do you mean it doesn't work? Can you be more specific. Have you tried putting in a few Debug.Logs in your code to check where it's going wrong? Use the comment button to comment thanks.

avatar image rinwatergirl · Mar 26, 2012 at 07:35 PM 0
Share

Well, it wouldn't create a new instance but my friend had his project done and pointed out what I did wrong so now the objects are being created. Sorry about taking so long to respond, I only have internet access at school and I'm only at school fomr monday to wednesday.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Mar 22, 2012 at 07:31 PM

I don't understand your question and what your problem is by your explanations. I don't even know the game before (so i had to google first).

The only thing that doesn't make sense is that if your bomb's movedirection is (0,-1,0), this condition doesn't make any sense: if(newPosition.y > -40)

Since the movedirection is negative the position can only get smaller. So either the position is greater than -40 from the beginning on so the bomb is destroyed in the first frame, or the bomb starts below -40 so the condition never can become true since the position is getting smaller.

I guess you have messed up your condition. Most people don't understand that -50 < -40

Beside that i can't see major mistakes in your code without knowing the actual issue.

Comment
Add comment · Show 1 · 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
avatar image rinwatergirl · Mar 26, 2012 at 07:37 PM 0
Share

Actually that wasn't my problem. I didn't have my object connected to the preFab I made for it.

I've got it working now, though but thanks for the help. Sorry it took so long, only connection I have to the internet is at school, and that is only $$anonymous$$onday to Wednesday.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to generate on click destroy enemies? 1 Answer

How do you destroy a prefab, but not the original object? 1 Answer

How do you use collision to log number of hits, while duplicates are being destroyed? 4 Answers

call a function when object when destroy 1 Answer

Array of different rigidbody 0 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