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 Menatombo · Mar 01, 2014 at 11:53 PM · npcitem pickupnpcsgathering

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.

 }
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

2 Replies

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

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.

Comment
Add comment · Show 3 · 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 Menatombo · Mar 02, 2014 at 01:16 AM 0
Share

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.

avatar image robertbu · Mar 02, 2014 at 01:26 AM 0
Share

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.

avatar image Menatombo · Mar 02, 2014 at 01:39 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · 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 Menatombo · Mar 02, 2014 at 01:10 AM 0
Share

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

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

21 People are following this question.

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

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


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