- Home /
Trying to destoy an object can a transform be hardcoded?
`var target : Transform;
var moveSpeed = 3;
var rotationSpeed = 3;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.Find("Cube").transform;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;`
This is the code I have to go towards the cube I want destroyed.
`function OnTriggerEnter (other:Collider){
if(other.gameObject.name == "Cube"){
Debug.Log("It went through");
Destroy(gameObject.Find("FCube"));
}
}`
This is the code I'm trying to mess with to remove the object. What I'm trying to do is have an NPC run over a cube and pick it up before the player does. I've been on this for a few days now and tried so many things.
What I'd like if someone could kindly explain to me...
Can I tag an object and then have an NPC character run at that object? And once they run over it... it is destroyed. (And any idea how that would be done.)
The problem is that there are two players. One is a player firing the cubes, and the other is the real player trying to gather the cubes as they are fired. The NPC is also trying to gather cubes and make points.
When the NPC or player gathers a cube as it stands now it gives the error that the transform has been removed and is still being called to. I just need to know how to remove the run over object from the scene, leaving the others, and still allowing pick up of the others.
Hope this makes some kind of sense.
}
Answer by robertbu · Mar 02, 2014 at 12:17 AM
First the fundamental problem is that just because an object is destroyed, the references to that object are not reset. So in your case, 'target' points at an object. When you destroy that object, your 'target' variable is no longer valid, but your code does not know and attempts to use that variable. There are a couple of approaches to the problem. The simplest is to reset target by finding the object anew each frame. GameObject.Find() is not a very efficient way to find an object and therefore you should look for an alternate. The typical alternate is GameObject.FindWithTag(). You would need to set the tag on the cube prefab to something unique. Here is a bit of info on tags and on the tag manager:
http://docs.unity3d.com/Documentation/Components/Tags.html
https://docs.unity3d.com/Documentation/Components/class-TagManager.html
One you have a tag on your blocks, you can just put this in the top of your update loop:
var go = GameObject.FindWithTag("Block");
if (go == null) return;
target = go.transform;
If the target from the previous frame is destoroyed, this code will assure that a new target is set. If no object exist in the scene, then the rest of the Update() function is ignored. This code assumes you name your tag 'Block'. Substitute as appropriate.
This code will find a block. If you game has many blocks shooting out, you may want your AI to find the nearest block. I leave that for you to write, but you can use GameObject.FindGameObjectsWithTage() to get a list of all the game object in the scene with a specific tag. Then you can look through them to find the nearest and set 'target' to that value. FindGameObjectsWithTag() is efficient enough to run every frame for any reasonable number of objects.
Thanks. I did look up a lot about tags and have been reading through the manuals and online help. (Which has a lot of information.) So, everything is tagged. Named it Cube. When I put your code in the first part of my update loop I get the error...
BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.
I'm going to check around to try to figure out what that means, but you've definitely given me a push in the right direction.
Sorry. I see target is a transform in your code. I edited the example code. If you are setting target to the transform, you need to be careful not to try and access the transform if the 'go' is null. This code returns when 'null' so it would be okay, but you may structure your code differently.
Thank you. This is exactly what I was hoping to accomplish. I was beginning to think it wasn't possible. Thanks again, now I can finally push on to the GUI and score boards.
Answer by Zentiu · Mar 02, 2014 at 12:10 AM
I don't know much about JS.
However, I believe when you fire cubes you instantiate them, correct? in that case the name of the cube is no longer "Cube" instaid its now called "Cube (Clone)" in the hierarchy. look for that name instaid and your code should work as far as I know.
again my knowledge of JS is extremely limited so don't hate me if my answers fails ya.
It's okay. :) I'm only trying UnityScript because I want to be a bit more proficient in it. But I got rid of the clone at the end with some code I found on here. So all names are the same. The problem is that once a cube is picked up the transform gets wiped out.
Your answer
Follow this Question
Related Questions
talking with a npc 0 Answers
Server + Client NPC Syncing issue 1 Answer
Rotate object in direction of movement 1 Answer
npc dialogue 0 Answers
How to make NPC Routine Movements? 1 Answer