I need your help please. I’m trying to combine two codes:
ar distance : float = 0;
var object1 : GameObject;
var object2= GameObject;
function Update ()
{
distance = Vector3.Distance(object1.transform.position, object2.transform.position);
}
and
var crate = GameObject.FindWithTag ("crate");
The reason is, I’d like to pick up a crate that is in a certain distance to the player. Picking up the crate works fine. The problem is after I dropped it I can’t pick it up again Now I’m hoping by combining the code that unity is going to refresh the distance again because it is not looking for the specific crate with its unique ID but for an object tagged “crate”.
The code snippets you’ve posted aren’t really enough for anybody to help you here, and the first is just plain broken.
Break down what you’re trying to do into a few steps, and figure out which step is causing you problems. It’s unclear what your real problem is (to me), but I don’t think you’re trying to solve it in the correct way.
You also don’t need to be doing this distance check in update() every single frame for every single crate, just do it when the player presses the “pick up” key or however you’re managing this. You could either attach the “pick up” script to the player/weapon, then do a raycast (like the typical “physics gun” that you see in a lot of games) to figure out what they’re looking at and how far away it is. Alternatively you could implement a “use” function on the crate prefabs that gets triggered when the player hits the pickup button within range of them. Without knowing what you’re doing, it’s impossible to really help.
ok … I see where you are comming from. I’m going to explain it in more detail.
The Sphere is the player. I like to pick up the crate. Here is the code to do that:
var distance : float = 0;
var object1 : GameObject;
var object2 : GameObject;
static var cratecount = 0;
function Update ()
{
name = "crate";
distance = Vector3.Distance(object1.transform.position, object2.transform.position);
if((distance < 0.9) Input.GetButtonUp("Grab_Object"))
{
cratecount += 1;
Debug.Log("grab it!");
Destroy(gameObject);
}
}
After I picked up the crate, the distance stays zero … I guess that ok because there is no crate left. But there is a message popping up in the Debug Log:
MissingReferenceExeption: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Now I’m dropping the crate. Here is the script again:
var object3 : GameObject;
function Update()
{
if(Input.GetButtonUp("Drop_Object") (distance_to_object.cratecount > 0))
{
var thePosition = transform.TransformPoint(Vector3.zero);
Instantiate(object3, thePosition, object3.transform.rotation);
distance_to_object.cratecount -= 1;
}
}
But after I droped the crate Unity won’t update the distance even when I move further away from the crate
Now what I like to do is, pick up the new crate. So I need Unity to “find” it so I can pick it up again. Thatswhy I like to combine the two codes. But when there is another way to do it I’m happy to learn something new. Could give an example code for what you said about checking the distance? I don’t know whether I understud completely what you said.
Okay. The first problem here is that you’re destroying the crate. I don’t think you want to do this. You want to modify the position of the crate to snap it in front of the player, not destroy it and then instantiate a new one in the correct position. This is why Unity is losing the reference to the crate, because (I assume) you’re setting the reference in the inspector before running the game then destroying the referenced item and instantiating a new one, which means you’ve lost the reference you had.
If you really want to stick with the approach you’ve got here, then yeah, you’re going to have to do something like GameObject.FindWithTag() to set the reference after you call Instantiate(). This isn’t a great idea, because not only are you complicating things by destroying and recreating the item but you’re going to have to modify this to support more than one crate or multiple types of items.
I would strongly suggest that instead of doing it this way, you instead perform a raycast (check Unity help or google for examples) when the player hits the “Grab_Object” key, which you can use to get a reference to the item that the player is looking at, filtered by tag to ignore things like terrain or non-pickupable(?!) items. You can then (for now) teleport that object in front of the player to make it look like they’ve picked it up, perhaps even parenting the picked-up item to the player so that it maintains its position automatically and disabling gravity on it to make it float, or however this type of thing is usually implemented. Then, when they hit the “Drop_Object” key you can unparent the item and switch on gravity to make it fall. Maybe even apply a force to it to let the player throw it.
Try the Unity wiki or searching the forum/internet, as I’m sure there’s working examples of this type of script to play with out there.
Also, do the Input testing first and then check distance inside that if(), otherwise you’re performing the distance check every single frame when you don’t need to. Not really important to your problem and it would be solved by raycasting instead of your approach, but it bugs me.
I do have another question though … If I wanted to pick up multiple crates I couldn’t use the technique you discribed could I? Because if I e.g. picked up 3 crates an then pressed teh “drop_object”-key the player would drop them all at once wouldn’t he?
Start with picking up and dropping a single cube before worrying about that. And no, the player will drop as many as you tell him to drop, as you’re the one designing and writing your code. You can have him dance the Robot and shoot cubes out of his ass if you like.
If you’re trying to create a system here where players “pick up” a cube like in Half Life 2 by looking at one and pressing a key, then you want to be using a raycast method, no question. Even if you just want the player to be able to look at a cube and press a key, then have that cube be destroyed and just increment a number to represent the fact that the player is now carrying cubes+1, you want to be raycasting to see what they’re trying to pick up. How you handle the actual carrying of the cube (does it float in front of the player, or is it just represented as an icon/number in an inventory) is entirely up to you.
edit: looks like I might’ve misunderstood what you’re doing here. If you just want the player to run over an item and pick it up, then you can just use collision events instead of messing around trying to work out distances. Add an OnCollision event handler to your crate prefab and the code inside it will be called when the player bumps into it.