Search Unity

how do i use destroy object correctly with my drop object script

Discussion in 'Scripting' started by radar089, Dec 17, 2014.

  1. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var ncurrentblock1 : int = 0;
    4. var nmaxblock1 : int = 10;
    5. var TowerPrefab : Transform;
    6. function Update () {
    7. if(Input.GetKeyDown("x"))
    8. {
    9. var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    10. var hit : RaycastHit;
    11. if (Physics.Raycast (ray, hit))
    12. Instantiate(TowerPrefab, hit.point, Quaternion.identity);
    13. }
    14. if (ncurrentblock1 < nmaxblock1)
    15. {
    16. Destroy (gameObject);
    17. }
    18. }

    So, in my fps you can destroy allot of object and equip objects also. Boxes, oil drums, whatever... the object hovers on the left hand side of the screen like a floating HUD of what's equipped. Only when you have your axe hammer out that is. I'm trying to have the script drop 10 of the object and then destroy the box or oil drum or whatever object it is that I attach this script too. This script only runs when the object is out with the hammer and if the object can be destroyed after 10 that's perfect. The player will have plenty more to equip. Right now the way it is it just destroys the object instantly on load. But it drops items fine.
     
    Last edited: Dec 17, 2014
  2. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    Thought this might help. Eventually all objects that float on the left will be a clearish blue or grey texture instead. So it will look like a hologram lol...
     

    Attached Files:

  3. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    Your script currently says:
    Destroy ncurrentblock1 when it is less than nmaxblock1

    You've haven't yet set ncurrentblock1 to = anything more than 0, so, it'll always destroy as soon as instantiated.
     
  4. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    I changed it to if ncurrentblock1 == nmaxblock1 and it still drops blocks after i drop 10 blocks. And I tried greater than. How does the script know to count each block I drop as ncurrentblock1? Is that maby the issue? This script is attached to that floating cube. So far all I can get working is I can drop my object prefabs by pressing x if the object is out with the axe. I just need to limit the amount of prefabs it can drop to 10.
     
  5. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    uhhhhhhh...
    Let's start over.

    You want to be able to drop blocks when you click a button (or do something)?
    If you've dropped 10 blocks, you want no more blocks to be able to be dropped?

    If so, then...You want something like the following. NOTE: This is C# code, I don't use java, but it should convert or work without any issue

    Code (CSharp):
    1.      
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class testBlocks : MonoBehaviour{
    6.     public int blocks;
    7.     void Update(){
    8.         if(Input.GetKey (KeyCode.X) && blocks < 10){
    9.             //Spawn prefab here
    10.             blocks++;
    11.         }
    12.     }
    13. }
    14.  
    15.  
     
  6. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    I got 3 compile errors.

    The name KeyCode does not exist in current context.

    The best overload method match for 'unity engine.Input.GetKey(string)' has some invalid arguments.

    Argument #1 cannot convert object expression to type string.
     
  7. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    Changing it to .GetKeyDown("x") fixed them. Now how do I add the prefab as a var? Public var Towerprefab : transform? I get a parsing error when I try that.
     
  8. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    How should I instantiate my object? The js way didn't work. As u can tell I know nothing about scripting but every reply helps me learn... I'm not sure what to put in place of your comment
     
  9. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    one sec, ill build a quick thing
     
  10. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    Brother I appreciate your help and patience with me. Its been 11 months since I first started working with unity. If I can get this drop object script finished then only things left to finish my game are buildings. Lots and lots of buildings.... my game is a fps with a item gathering and dropping element. Less like mine craft more like call of duty with a building factor.
     
    Last edited: Dec 17, 2014
    darkAbacus247 likes this.
  11. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    Create empty scene, attach this to gameobject, again C# but this time you shouldn't have issues.
    You'll have to figure out the java part, I loath java.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic; //TODO: '.Generic' gives access to the "List" function.
    4.  
    5. public class testThing : MonoBehaviour {
    6.     List<GameObject> cubes = new List<GameObject>(); //TODO: Declares list
    7.     int cubeCount;
    8.  
    9.     void Update(){
    10.         if(cubes.Count < 10){
    11.             if(Input.GetMouseButtonDown(0)){
    12.                 cubes.Add (GameObject.CreatePrimitive (PrimitiveType.Cube)); //TODO: One of several ways to instanciate objects.
    13.             }
    14.         }
    15.         else{
    16.             Debug.LogError("You've already spawned 10 cubes, what now?");
    17.         }
    18.     }
    19. }
    20.  
     
  12. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    Note: Line 12 also adds the object to the list (in addition to creating the object), if you decide to go with another way of instantiating you'll have to account for this. Not a big issue just take it into account :)
     
  13. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    OK let me fiddle with this and see what I can do. What did you mean by figure the java part out?
     
  14. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    That bit of code is in c#, so if you need it to be in java, you'll have to convert it...
    There's a bunch of sites that can do it for you though. I unfortunately know very little about the java world :/
     
  15. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    OK so your script runs with no errors but it does nothing on my end. I have no way of choosing the object.how would it be changed from create primitive cube to any object? It has to hit at the rycast point.
     
  16. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    It also didn't drop a cube. How do you say "var tower prefab : transform; in #c ?

    I need it to do this js line

    Instantiate(tower prefab,hit.point,quanterion,identity);
     
  17. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    Or, can I specify what object it is in the script? And make multiple altered scripts for each object?
     
  18. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    It def. works, so you've done something incorrectly maybe. Follow these steps :)

    1. start new scene
    2. attached script to empty game object
    3. press play
    4. click the left mouse button

    A box(s) will spawn in the scene, watch the hierarchy you'll see this happen.
    Once you've spawned 10 boxes it'll give you an error code, this is where you'll put your code in (Just double clock the code it'll open the script right where you need to be).

    You can use instanciate.whatever in place of line 12...you just have to use it in the proper context is all.
     
  19. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    OK I'll try again maby I did something weird lol
     
  20. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    Also, how does the script know where to place the cube?
     
  21. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    Alright. So this is what happens. I guess I didnt notice this before in my main scene. But it does create cubes. I guess it automaticly makes them spawn at 0,0,0. So I'm going to try and add my instantiate bit of code at line 12 like you said.
     
  22. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    I'm getting a parsing error when I try and make a variable.

    Var TowerPrefab : Transform;

    Is that the wrong way to do it in c? Bassicly the JavaScript I originally posted allowed you to drag an object prefab into the script in the inspector. And then it instantiated that prefab. That's what I need this to do next. I can't figure out how to add the prefab part in c. The js way was var towerprefab... etc.
     
    Last edited: Dec 19, 2014
  23. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    yep, c# you would want to write:
    Code (CSharp):
    1. //Outside of structures
    2. public float myPubFloat = 0f; //Can be called from other scripts
    3. float myFloat = 0f; //No public def. means it's private
    4. private float myPrivFloat; //Private declaration, same as 'myFloat'
    5.  
    6. //OR
    7.  
    8. //INSIDE STRUCTURES
    9. float myFloat = 0f; //Same as above, -declare private/public
    10.  
    11. //And
    12. var myOtherFloat = 0f;
    13.  
    14. /*
    15. 'var' automatically figures out what the statement is and applies it. So, if your 'myOtherFloat' = "Hi"; it would assume it's a string and not a float. Keep this in mind
    16. */
     
    Last edited: Dec 21, 2014
  24. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    OK wait, I'm sorry I don't follow. Which of that bit is allowing a prefab? What is "of" is that what my orefab would be?
     
  25. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    Sry I'm sure you getting a headache... I don't get what that code says. Can you explain a lil more?
     
  26. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    OK, what did I do wrong lol?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public float myPubFloat = of;
    5. float myFloat = of;
    6. private myPrivFloat;
    7.  
    8. public class newestdropobj : MonoBehaviour {
    9.     var myOtherFloat = of;
    10.     List<GameObject> cubes = new List<GameObject>();
    11.     int cubeCount;
    12.  
    13.     void Update(){
    14.         if(cubes.Count < 10){
    15.             if(Input.GetMouseButtonDown(0)){
    16.                 Instantiate(TowerPrefab, hit.point, Quaternion.identity);
    17.  
    18.             }
    19.         }
    20.         else{
    21.             Debug.LogError("You already spawned 10 Cubes, What now?");
    22.         }
    23.     }
    24. }
    25.  
     
  27. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    I didn't know what to do after instantiate. And I'm not sure where to place the var you listed.
     
  28. radar089

    radar089

    Joined:
    Jan 2, 2014
    Posts:
    216
    Please don't give up on me. i downloaded a c# noob tutorial PDF to read. I'm doing all this via a 3g S***ty phone. im trying to understand what you said in your script. If you can exain more.you are helping me learn.
     
  29. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    No worries, I'm often working so my responses are delayed, sometimes quite delayed... We all have to start somewhere, I myself started coding just over a year ago. Give or take a month. Anyway, I'm happy to offer what help I can, though the forums are intended for quick type help. So I'd recommend starting a conversation with me directly so that we don't flood the general Que with shenanigans.

    Addressing your current question though, in your code response it looks like you've used the letter "o" instead of the number "0" for your 'public myPubFloat = of", which should automatically through an error code, because technically your requesting that a float == a string, which is not possible :) I'm assuming though this was an auto-incorrect.

    Looking further, your instantiate looks correct but the value 'hit.point' isn't determined within this script, so of course the system will dislike this as well. Right now we have no idea what 'hit.point" is, is it a number? Is it a string? ...is it a rabbit? It must be defined, specifically as a vector position, which i imagine you'll want to use a raycast to get the exact location of where you've clicked, given that the click position contains a collider to be clicked on in the first place.

    Truthfully some of this gets quite confusing and in depth, if you haven't already I'd highly recommend looking through unitys built in scripting help, which is under the help menu within Unity. I almost always spend an hour or better in there a day. But again, feel free to start a conversation with me and I'll help when available, best of luck!
     
    Last edited: Dec 21, 2014
  30. darkAbacus247

    darkAbacus247

    Joined:
    Sep 7, 2010
    Posts:
    251
    Note line 6, my bad i forgot to include the "float" in my own code script, I've since edited the code...haha sorry...