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 toosp1ffy · Sep 30, 2010 at 03:18 PM · instantiateprefabscaleresize

Get scale of instantiated prefab and resize

Hello All-

I am new to Unity scripting and am working on a learning project.

In my scene, I have an object that when it gets hit (using the raycasting system) instantiates 2 new prefabs. With the current script, the newly instatiated objects are smaller than the original object hit.

Here's the problem: when I hit one of the new, smaller instatiated objects, it produces 2 new objects of the same size, not smaller than the object just hit. (The general idea being that whatever object you hit produces 2 new objects of a smaller size - so in theory you can keep making smaller and smaller objects)

I'm guessing that I'm not getting the scale of the object hit and instead keep getting the scale of the original prefab. Help please?

Thank you!

Here's the code:

if (Physics.Raycast ( transform.position, gunDirection, gunHit, gunRange) ) { print ("Hit something");

     //Add an explosion
     explosionClone = Instantiate (explosion, gunHit.point, transform.rotation);

     // Did we hit something with a rigidbody?
     if (gunHit.rigidbody)
     {
         print ("I hit a rigidbody");
         // Apply force to the object
         gunHit.rigidbody.AddForceAtPosition (gunForce * gunDirection, gunHit.point);

         for (var i=0; i<2; i++) {

             var hitScale = transform.localScale;
             print (hitScale);

             // Make new barrel                  
             respawnClone = Instantiate (respawnPrefab, gunHit.point, transform.rotation);

             // Rescale the new barrel
             var smaller = hitScale * 0.9;
             print (smaller);
             respawnClone.transform.localScale = smaller;

             // Add to counter
             barrelCounter.barrelsOn ++;
         }
     }

 }

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 Ray-Pendergraph · Sep 30, 2010 at 03:54 PM 0
Share

Wouldn't you get the localScale of the thing you hit from the RaycastHit (gunHit) not this.transform? $$anonymous$$aybe I am missing something.

1 Reply

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

Answer by toosp1ffy · Oct 13, 2010 at 07:59 PM

Yes, thank you. (I misunderstood what the "this" was referring to in the script. Dumb noob mistake) Here's the working version:

if (Physics.Raycast ( transform.position, gunDirection, gunHit, gunRange) ) { print ("Hit something");

     //Add an explosion
     explosionClone = Instantiate (explosion, gunHit.point, transform.rotation);

     // Did we hit something with a rigidbody?
     if (gunHit.rigidbody)
     {
         print ("I hit a rigidbody");
         // Apply force to the object
         gunHit.rigidbody.AddForceAtPosition (gunForce * gunDirection, gunHit.point);

         for (var i=0; i<2; i++) 
         {
             // Make new barrel                  
             respawnClone = Instantiate (respawnPrefab, gunHit.point, transform.rotation);

             // Rescale the new barrel
             respawnClone.transform.localScale = gunHit.transform.localScale * 0.8;

             // Add to counter
             barrelCounter.barrelsOn ++;
         }
     }

 }

Comment
Add comment · Show 2 · 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 vestax_ion · Jul 14, 2011 at 09:42 AM 0
Share

Could you please post the complete code with the variables respawnClone and other vars so i can understand what is happening? I am learning too, and as a noob i dont understand as the code is not complete. Thanks!

avatar image vickygroups · May 27, 2014 at 03:32 AM 0
Share

when i tried something like this it gave me an error that i was trying to access a non-static member . . . i need to shrink my prefab when enemy hits come on . . .

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

2 People are following this question.

avatar image avatar image

Related Questions

How do I scale an object or prefab to a size in Unity units? 1 Answer

Prefab instantiated wrong scale on mobile 0 Answers

Instantiated prefab scale is wrong. 2 Answers

Instantiate prefabs scaled 1 Answer

When I instantiate a 2D bullet it's BoxCollider2D fails verification. 0 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