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 carter-carl30 · Mar 01, 2012 at 12:42 AM · destroyshoot

Help with script (limiting gameobject but when destroyed allowing again)

Hi all, i'm a complete newbie to Unity and trying to get to grips with it. I can't code to save my life (at the moment).

I have made a breakout style game (WIP) and I have the following issue I can't get past:

I am using this shooting script (from another post on here) to fire my "Bullet" (ball in game):

var thePrefab : GameObject;

var MaxObjectCount = 0;

function Update () {

 if(Input.GetButtonDown("Jump")){

 var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);

    instance.name = "Bullet";

      MaxObjectCount += 1;

      checkhit();  

 }

}

function checkhit(){

 if(MaxObjectCount == 2){

    Destroy(GameObject.Find("Bullet"));

    MaxObjectCount -=1;

 }

}

this kinda works (not ideal) to let the player ONLY fire 1 ball (which is what I wanted). Onto my problem:

Using this script (on my floor) destroys the ball when the player misses and it hits the floor:

function OnCollisionEnter ( collision : Collision) {

if (collision.collider.name == "Bullet")

Destroy (GameObject.Find("Bullet"));

}

The problem I have is that once the ball is destroyed for the first time (after hitting the floor) I can't fire any more at all. I don't know what to add to the shooting script to make it allow the player to fire again (just one ball at a time again)

any help would be appriciated greatly!

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

1 Reply

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

Answer by immersivegamer · Mar 01, 2012 at 06:40 AM

Ok, that was a big pain. Have to rewrite everything again. At least I know what I'm going to be writing.

Haven't tested your scripts out, but they are fine. The problem is in the logic of the code. In the update() function, when ever a second bullet is created it adds to the var MaxObjectCount, however when it gets to 2 then in the checkhit() function, the last object to be created will be destroyed. Now here is the important part, in the check()after destroying the object they decrease the value of MaxObjectCount by one. Your collision check on the floor does not do that. So what happens is that when the bullet hits the floor MaxObjectCount become 2. This means that when you try to fire the next time check() will be called and destroy the object imdeiatlly. At least that is how I understand it.

So what you need to do is to update the variable from the collision check function.

//This is the object that has the shoot script on it. var shootObject : GameObject;

function OnCollisionEnter ( collision : Collision) {

if (collision.collider.name == "Bullet"){

 //replace both 'ScriptName' with the  name of the script that has your code in it.
 var shootScript : ScriptName = shootObject.GetComponent(ScriptName);

 Destroy (GameObject.Find("Bullet"));

 //modifies the var.
 shootScript.MaxObjectCount -= 1;

} }

Hopefully I didn't mess it up this time around. Someone will have to speak up if I forgot something.

As I said haven't tested anything, though that should fix your problem. If it doesn't then I'll test out all the scripts when I have the time.

Comment
Add comment · Show 4 · 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 carter-carl30 · Mar 01, 2012 at 09:30 PM 0
Share

Thank you very much immersivegamer that works perfectly :D

I never even considered my floor colision script, makes sense now (at least the logic of it, the code still makes my headspin until I can learn it (hopefully)

avatar image immersivegamer · Mar 02, 2012 at 04:02 AM 0
Share

Glad that it worked, was worried that I might have interpreted the code wrong when I was looking at without testing it. Also sorry if that answer looked confusing, seems like my first answer got merged with the second, I'll edit it now.

avatar image Kleptomaniac · Mar 02, 2012 at 06:09 AM 0
Share

@Carter: If the answer immesivegamer gave you was correct, you should acknowledge this by accepting his answer.

avatar image carter-carl30 · Mar 02, 2012 at 08:52 PM 0
Share

@kleptomaniac - thanks, I wasn't aware I had to do that :)

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

6 People are following this question.

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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Help with game, stopping player cheating (script help) 1 Answer

Shoot. Just Shoot 5 Answers

Destroy gameObject on collision/time? 0 Answers

Rocket to disappear after a few seconds? 2 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