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 Digital-Phantom · Feb 02, 2015 at 03:32 PM · javascriptinstantiatetransformclonedestroy object

Following Object can't find newly Instantiated Object (Solved)

I have 2 objects in my game, a sphere and a cube. The sphere starts at position A and the cube moves towards it using the following script -

 #pragma strict
 
 public var victim : Transform;
 private var navComponent : NavMeshAgent;
 
 function Start()
 {
     navComponent = this.transform.GetComponent(NavMeshAgent);
 }
 
 function Update()
 {
     if(victim)
     {
         navComponent.SetDestination(victim.position);
     }
 }
 

The sphere has a script on it that allows it to be destroyed and instantiated at a new location -

 #pragma strict
 
 public var resourceSpawnPoints : Transform[]; //Makes a list of transforms in my game
 public var randomPos : int; //This denotes that the variable randomNum will be a whole number
 
 public var resourceType : GameObject;
 
 function OnTriggerEnter(thing: Collider)
 {
     if (thing.tag == "Harvester")
     {
            ResourceRespawn();
       }
 }
 
 function ResourceRespawn()
 {
     randomPos = Random.Range(0,4); // generates random number between 0 and 3.
     
     transform.position = resourceSpawnPoints[randomPos].transform.position;
     //Assigns the number generated in the previous line to the 'spawnPoints' variable
     //Which then dictates which spawnPoint(location) is used (spawnPoint1, spawnPoint2, spawnPoint3)
 
        Instantiate(resourceType, resourceSpawnPoints[randomPos].position, resourceSpawnPoints[randomPos].rotation);
 
     Destroy (gameObject);
 }
 

My problem is that once the cube/harvester reaches the sphere it activates the sphere's destroy/instantiate script but then stops. It doesn't then move towards the newly instantiated sphere.

Any suggestions as to why this happens (or doesn't happen to be more precise)

???

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

1 Reply

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

Answer by Mmmpies · Feb 02, 2015 at 03:46 PM

All that's happening is you have the victim as a public transform and as soon as you destroy that game object it's gone. The new GameObject is something different.

Hold a reference to the first script in the second one, add a tag to the victim prefab. Also add a function in the the first script to search for gameObjects with that tag and set that as the new victim. Then, using the reference to first script call that new function after the new victim has been instantiated.

My JS is poor but I could probably put the code together if you're struggling.

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 Digital-Phantom · Feb 02, 2015 at 03:50 PM 0
Share

I'm familiar with FindGameObjectWithTag("insertTag") tried a few variations of this but seemed to make things worse...lol

I'll have another try using your suggestions

avatar image Digital-Phantom · Feb 02, 2015 at 04:01 PM 0
Share

I actually had a script that did pretty much what you suggested. I'd tries using this before but couldn't get it to work. Then realised that it relied on a start function (hence only got called once) As soon as I changed it to an Update function my follow object now happily moves around to whatever place the sphere spawns to.

 private var targetPosition : Vector3;
  
 private var directionNavAgent : Nav$$anonymous$$eshAgent;
 
 function Update() //Change this back to a START function if it doesn't work
 {
      $$anonymous$$oveToClosest();
 }
  
 function $$anonymous$$oveToClosest()
 {
      directionNavAgent = GetComponent(Nav$$anonymous$$eshAgent);
      FindClosestTarget();
      directionNavAgent.destination = targetPosition;
 }
   
  
 function FindClosestTarget () : GameObject
 {
     var gos : GameObject[];
      gos = GameObject.FindGameObjectsWithTag("Resource"); 
      var closest : GameObject; 
      var distance = $$anonymous$$athf.Infinity; 
      var position = transform.position; 
  
      for (var go : GameObject in gos)
 { 
        var diff = (go.transform.position - position);
        var curDistance = diff.sqr$$anonymous$$agnitude; 
        if (curDistance < distance)
            { 
                closest = go; 
                distance = curDistance;
            } 
 } 
     targetPosition = closest.transform.position;  
     return closest;
 }
 

Something about your suggestion just triggered my to think about it.

$$anonymous$$any thanks :)

avatar image Mmmpies · Feb 02, 2015 at 04:03 PM 1
Share

No problem @DigitalPhantom, glad it helped.

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

20 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

Related Questions

Error CS0029 - Trying to destroy an object and then instantiate it again? 1 Answer

How to add random force/Rotation to bulletEject. 1 Answer

Instantiating a prefab question 1 Answer

Instantiate Error (BCE0023) 1 Answer

Cannot add Instaniated gameObjects to a GameObject[] using Javascript. Error: "NullReferenceException: Object reference not set to an instance of an object" 2 Answers


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