- Home /
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??
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.
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613065419im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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