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 12, 2015 at 06:29 PM · gameobjecttransformsizeduplicatelocalscale

Can't get Object size to Reset to Original

I have a game object that grows for a certain amount of time, then once it reaches a set size it duplicates itself. However... I'd like the duplicated object to be reset back to the original size (so it can go through the grow process again before duplicating itself)

 #pragma strict
 
 public var circleRadius : float;
 public var waitTime : float = 2f;
 private var hasDuplicated : boolean = false;
 private var fullyGrown : boolean = false;
 
 
 
 function Update()
 {
     if(fullyGrown == false)
     {
         if(transform.localScale.x >= 3.0 && transform.localScale.y >= 3.0 && transform.localScale.z >= 3.0 )
         {
             fullyGrown = true;
             StartCoroutine(WaitAndDuplicate());
         }
         else
         if(transform.localScale.x < 3.0 && transform.localScale.y < 3.0 && transform.localScale.z < 3.0 )
         {
             transform.localScale += Vector3(0.001,0.001,0.001); // Increase object size by .01 per frame
         }
     }
 }
 
 function WaitAndDuplicate()
 {
     if(hasDuplicated == false)
     {
         yield WaitForSeconds(waitTime); var randomOffset : Vector3 = Random.insideUnitCircle * circleRadius; randomOffset = Vector3(randomOffset.x, randomOffset.z, randomOffset.y); Instantiate(gameObject, transform.position + randomOffset, transform.rotation);
         hasDuplicated = true;
     }
 }
 

At the moment, the game object grows to full size, waits 2 seconds then duplicates itself. The trouble is the duplicate is already fully grown (which I don't want)

How do I get each new duplicate to start off at the original size?

Comment
Add comment · Show 2
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 rob5300 · Feb 12, 2015 at 06:35 PM 1
Share

When you instantiate, it returns the object that was created. There you can just set the scale to what you want it to start at. Or change the prefab to have the correct scale.

avatar image Digital-Phantom · Feb 12, 2015 at 07:05 PM 0
Share

Obviously I'm missing something here. Changed the script to -

 #pragma strict
 
 public var circleRadius : float;
 public var waitTime : float = 2f;
 private var hasDuplicated : boolean = false;
 private var fullyGrown : boolean = false;
 
 public var newCrystal : GameObject;
 
 function Update()
 {
     if(fullyGrown == false)
     {
         if(transform.localScale.x >= 3.0 && transform.localScale.y >= 3.0 && transform.localScale.z >= 3.0 )
         {
             fullyGrown = true;
             StartCoroutine(WaitAndDuplicate());
         }
         else
         if(transform.localScale.x < 3.0 && transform.localScale.y < 3.0 && transform.localScale.z < 3.0 )
         {
             transform.localScale += Vector3(0.001,0.001,0.001); // Increase object size by .01 per frame
         }
     }
 }
 
 function WaitAndDuplicate()
 {
     if(hasDuplicated == false)
     {
         yield WaitForSeconds(waitTime); var randomOffset : Vector3 = Random.insideUnitCircle * circleRadius; randomOffset = Vector3(randomOffset.x, randomOffset.z, randomOffset.y); Instantiate(newCrystal, transform.position + randomOffset, transform.rotation);
         hasDuplicated = true;
     }
 }
 

The newCrystal : GameObject has the prefab on it in the Inspector. And as I'm now instantiating the prefab (XYZ scale of 1) shouldn't the instantiated object have an XYZ scale of 1 also ???

1 Reply

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

Answer by InvincibleCat · Feb 12, 2015 at 07:37 PM

Just add private var originalScale : Vector3 = Vector3.zero;

Then on the Start method:

 function Start()
 {
     originalScale = transform.lossyScale;
 }

And when replace your instanciate method with:

  var newCrystalGO : GameObject = Instantiate(newCrystal, transform.position + randomOffset, transform.rotation);
  newCrystalGO.localScale = originalScale;

And the full script will be

 #pragma strict
  
 public var circleRadius : float;
 public var waitTime : float = 2f;
 private var hasDuplicated : boolean = false;
 private var fullyGrown : boolean = false;
  
 public var newCrystal : GameObject;
  
 private var originalScale : Vector3 = Vector3.zero;
 
 function Start()
 {
     originalScale = transform.lossyScale;
 }
 
 function Update()
 {
     if(fullyGrown == false)
     {
         if(transform.localScale.x >= 3.0 && transform.localScale.y >= 3.0 && transform.localScale.z >= 3.0 )
         {
             fullyGrown = true;
             StartCoroutine(WaitAndDuplicate());
         }
         else
             if(transform.localScale.x < 3.0 && transform.localScale.y < 3.0 && transform.localScale.z < 3.0 )
             {
                 transform.localScale += Vector3(0.001,0.001,0.001); // Increase object size by .01 per frame
             }
     }
 }
  
 function WaitAndDuplicate()
 {
     if(hasDuplicated == false)
     {
         yield WaitForSeconds(waitTime); 
         var randomOffset : Vector3 = Random.insideUnitCircle * circleRadius; 
         randomOffset = Vector3(randomOffset.x, randomOffset.z, randomOffset.y); 
         var newCrystalGO : GameObject = Instantiate(newCrystal, transform.position + randomOffset, transform.rotation);
         newCrystalGO.transform.localScale = originalScale;
         hasDuplicated = true;
     }
 }

Cheers

Comment
Add comment · 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

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

Scale (Transform.localscale) a gameobject based on ARFaceAnchor anchorData 0 Answers

Modify Sprite size to reflect parent element 1 Answer

Adding a prefab to gameObject at a certain position in runtime 0 Answers

Creating new Transform from existing objects Transform to Instantiate object 1 Answer

Animation issue (OnTriggerEnter) ***SOLVED*** 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