Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Flaviof · Aug 13, 2016 at 04:33 PM · prefabobject

Instantiating Prefab

So, im following this post: http://answers.unity3d.com/questions/685405/mass-place-trees-tagging.html.

And all working good. But i want to chance something, and i need some help. Im very newbie on this.

 #pragma strict
  
  public var player : Transform;
   
  private var paryTrees : TreeInstance[];
  private var pvecTerrainPosition : Vector3;
  private var pvecTerrainSize : Vector3;
  private var pgobTreeCollide : GameObject;
  private var pvecCollideScale : Vector3;
  private var pbooCollideWithTrees : boolean = false;
   
  function Start()
  {
      // Get the terrain's position
      pvecTerrainPosition = Terrain.activeTerrain.transform.position;
       
      // Get the terrain's size from the terrain data
      pvecTerrainSize = Terrain.activeTerrain.terrainData.size;
      // Get the tree instances
      paryTrees = Terrain.activeTerrain.terrainData.treeInstances;
       
      // Get the invisible capsule having the capsule collider that makes the nearest tree solid
      pgobTreeCollide = GameObject.Find("Tree"); // This is a capsule having a capsule collider, but when the flier hits it we want it to be reported that the flier hit a tree.
       
      // Are there trees and a tree collider?
      if ((pgobTreeCollide != null) && (paryTrees.length > 0))
      {
          // Set a flag to make this script useful
          pbooCollideWithTrees = true;
          // Get the original local scale of the capsule. This is manually matched to the scale of the prototype of the tree.
          pvecCollideScale = pgobTreeCollide.transform.localScale;
      }
      // No need to use this script
      else 
      {
          Debug.LogWarning( "NO CAPSULE NAMED TREE FOUND, OR NO TERRAIN TREES, DESTROYING SCRIPT..." );
          Destroy(this);
      }
      
      // has the player been assigned in the Inspector?
      if ( !player ) 
      {
          Debug.LogWarning( "NO PLAYER OBJECT IN THE INSPECTOR, DESTROYING SCRIPT..." );
          Destroy(this);
      }
  }
   
  function Update() 
  {
      var L : int;
      var triTree : TreeInstance;
      
      //var vecFlier : Vector3 = sctFly.svecXYZ; // My protagonist's position, passed by a static variable in a script called sctFly.
      var vecFlier : Vector3 = player.position; // using the player transform, dropped in the inspector
      
      var fltProximity : float;
      var fltNearest : float = 9999.9999; // Farther, to start, than is possible in my game.
      var vec3 : Vector3;
      var vecTree : Vector3;
      var intNearestPntr : int;
       
      // Test the flag
      if (pbooCollideWithTrees == true)
      {
          // Find the nearest tree to the flier
          for (L = 0; L < paryTrees.length; L++)
          {
              // Get the tree instance
              triTree = paryTrees[L];
              // Get the normalized tree position
              vecTree = triTree.position;
              // Get the world coordinates of the tree position
              vec3 = (Vector3.Scale(pvecTerrainSize, vecTree) + pvecTerrainPosition);
              // Calculate the proximity
              fltProximity = Vector3.Distance(vecFlier, vec3);
              // Nearest so far?
              if (fltProximity < fltNearest)
              {
                  // Remember the nearest
                  fltNearest = fltProximity;
                  // Remember the index
                  intNearestPntr = L;
              }
          }
          // Get the closest tree
          triTree = paryTrees[intNearestPntr];
          // Get the normalized tree position of the closest tree
          vecTree = triTree.position;
          // Get the world coordinates of the tree position
          vec3 = (Vector3.Scale(pvecTerrainSize, vecTree) + pvecTerrainPosition);
          // Scale the capsule having the capsule collider that represents a solid tree
          pgobTreeCollide.transform.localScale = (pvecCollideScale * triTree.heightScale);
          // Add some height to position the capsule correctly on the tree
          vec3.y += pgobTreeCollide.transform.localScale.y;
          // Position the capsule having the capsule collider at the nearest tree
          pgobTreeCollide.transform.position = vec3;
      }
  }

on the object "Tree" im using this:

 #pragma strict
 
 var Collected : boolean = false;
 
 private var showGui : boolean = false;
 
 var recurso : Transform;
 var player : Transform;
 
 function Update()
 {
 
     if(Collected == true)
     {
         MakeRecurso();
     }
 }
 
 function MakeRecurso()
 {
     Instantiate (recurso, player.transform.position, Quaternion.identity);
     Collected = false;
     showGui = false;
 }

and this:

 #pragma strict
 
 var Health = 100;
 
 function Update ()
 {
     if(Health <= 0)
     {
         
         Dead();
         
     } 
 }
 
 function ApplyDammage (TheDammage : int)
     {
         Health -= TheDammage;
         var ManageScript : ManageWood = GameObject.FindWithTag("Tree").GetComponent(ManageWood);            
         ManageScript.Collected = true;
     
     }
 
     function Dead()
     {
     
         Destroy(gameObject);
     
     
     }
 
 

Everything works great, no problems. When I approach a tree the object move to that tree and I hit the object and it will drop logs. Perfect!

But as you can see this object has a life stats. And it disappears when it is destroyed.

I need to modify my script so that this object is instantiated from a Prefab. When the script detects that the object is destroyed it will instantiate a new one.

Can anyone ELI5 me please?

Comment
Add comment · Show 1
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 afonsolfm · Aug 13, 2016 at 08:41 PM 0
Share

I'd recommend you shorten your code a bit to a $$anonymous$$imum. It's hard for anyone to read all of that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zedock · Aug 14, 2016 at 01:21 AM

So the only point in which you are instantiating anything is on the Tree script - line 21.

You currently have: Instantiate (recurso, player.transform.position, Quaternion.identity); I don't know if this would fix you problem, but I would start by assigning the Instantiate function to a variable. Like so (note I do not work with javascript so the syntax for the variable may be wrong): var recursoObject : GameObject = Instantiate (recurso, player.transform.position, Quaternion.identity) as GameObject;

This will make it easier to create an delete any prefab.

If this is not what you are looking for, sorry for this but please note it was unclear about which section to look for in your massive paste of code. If you still need help I would begin by only providing the absolute necessary code.

Comment
Add comment · Show 4 · 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 Flaviof · Aug 14, 2016 at 03:03 PM 0
Share

i solved this problem checking if the "tree" obj exist, if not just instantiate one.

but i have to destroy the tree now and not the object.

As you can see here:

 triTree = paryTrees[intNearestPntr];

i pary the "tree" obj to the tree that is closest to the player. When the player hit the tree drop wood, and when the heath are done the "Tree" obj (Read collider obj) its destroid.

But what i rly need is that the tree that the "Collider obj" are attached get destroyed. Not the "Collider obj". As you can see here

  function ApplyDammage (TheDammage : int)
      {
          Health -= TheDammage;
          var $$anonymous$$anageScript : $$anonymous$$anageWood = GameObject.FindWithTag("Tree").GetComponent($$anonymous$$anageWood);            
          $$anonymous$$anageScript.Collected = true;
      
      }
  
      function Dead()
      {
      
          Destroy(gameObject);
      
      
      }


So, where im doing Destroy(gameObject);

i have to do some Destroy(parytreeObject).

$$anonymous$$y problem is that the "paryTree" is defined in another script that is attached to another object.

So, i need this script to ins$$anonymous$$d of Destroy the "collider object" destroy the Tree that the "Collider object" is attached at.

Thats very complex to me. I have no idea what to do.

Put that aside, you comment help me a lot to make a better code. Thank very much.

avatar image Flaviof · Aug 15, 2016 at 01:18 PM 0
Share

Ok, i know how to instantiate a clone. But i still stuck, what i rly need i how to Destroy that Tree that the collider is attached.

Insted of Destroy(gameObject);

how can i destroy the tree that this object is attached at that momment?

thank you if you can help me or at least give a tip.

avatar image Fair-Strides · Aug 15, 2016 at 01:23 PM 0
Share

In addition to @Zedock's code to help you instantiate from a prefab, I don't see any particularly reason you couldn't copy the instantiation code again into the function Dead(), followed again with the following after you instantiate:

      var $$anonymous$$anageScript : $$anonymous$$anageWood = GameObject.FindWithTag("Tree").GetComponent($$anonymous$$anageWood);            
      $$anonymous$$anageScript.Collected = false;
      // This next bit might not be required...
      $$anonymous$$anageScript.recurso = // Something along the lines of "$$anonymous$$anageWood.transform;"
avatar image Flaviof Fair-Strides · Aug 15, 2016 at 06:50 PM 0
Share

I did, but the problem is that the Terrain tree remains, so the player can hit that tree forever... Im trying to destroy the Terrain Tree when "Collider Object" (that is attached to that tree) get a number of hits.

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

6 People are following this question.

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

Related Questions

How can I tell what prefabs a child object (script) is in? 2 Answers

How To Make Objects Appear Lit Without Using Lighting 2 Answers

Audio, play when laser hits object 1 Answer

Cannot assign prefab in Inspector? 2 Answers

How to reset a prefab used in object pool ? 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