#pragma strict
private var ncurrentblock1 : int = 0;
var nmaxblock1 : int = 10;
var TowerPrefab : Transform;
function Update () {
if(Input.GetKeyDown("x"))
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
Instantiate(TowerPrefab, hit.point, Quaternion.identity);
}
if (ncurrentblock1 < nmaxblock1)
{
Destroy (gameObject);
}
}
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.
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…
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.
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.
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
using UnityEngine;
using System.Collections;
public class testBlocks : MonoBehaviour{
public int blocks;
void Update(){
if(Input.GetKey (KeyCode.X) && blocks < 10){
//Spawn prefab here
blocks++;
}
}
}
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.
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.
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
one sec, ill build a quick thing
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.
1 Like
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.
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //TODO: '.Generic' gives access to the "List" function.
public class testThing : MonoBehaviour {
List<GameObject> cubes = new List<GameObject>(); //TODO: Declares list
int cubeCount;
void Update(){
if(cubes.Count < 10){
if(Input.GetMouseButtonDown(0)){
cubes.Add (GameObject.CreatePrimitive (PrimitiveType.Cube)); //TODO: One of several ways to instanciate objects.
}
}
else{
Debug.LogError("You've already spawned 10 cubes, what now?");
}
}
}
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 
OK let me fiddle with this and see what I can do. What did you mean by figure the java part out?
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 
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.
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);
Or, can I specify what object it is in the script? And make multiple altered scripts for each object?
It def. works, so you’ve done something incorrectly maybe. Follow these steps 
- start new scene
- attached script to empty game object
- press play
- 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.
OK I’ll try again maby I did something weird lol
Also, how does the script know where to place the cube?