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 Davon92 · Mar 24, 2015 at 12:48 PM · javascriptraycastclonesdecalsobjectpool

When Object Pooling Bullet Decal Arrays in UnityScript, My code use the original prefabs instead of the instantiated cloned prefabs

fairly new to coding and the unity fourms. im having a problem at the moment and i want to know if anyone can help... im object pooling and array of bullet hole decal gameobjects into an array and instantiating a Random gameobject with a specific pooled amount just to test. When I try to place my decals and set them active my code seems to be using the original prefabs in the Hierarchy. When taken out of the hierarchy, they use nothing at all instead of the pooled clones. here is the code snippet

 var shotOrigin:Transform;
 var shotDirection:Transform;
 var shotDistance:float;
 var fireRate:float;
 var hit : RaycastHit;
 var bulletHoleDecals:GameObject[];
 var poolAmount = 2;
 var triggerDown:boolean;
 var Decals:GameObject[];
 var vex:Vector3;
 
 function Start () 
 {
     var bulletHoleDecals = new Array();
      
     //Debug.Log(bulletHoleDecals.Length);
     triggerDown=false; 
    
     for(var i = 0;i<poolAmount;i++)
     {
         var obj:GameObject=GameObject.Instantiate(Decals[Random.Range(0,2)]);
         obj.SetActive(false);
         bulletHoleDecals.Add(obj);
     }
 }
 
 function Update () 
 {
     IsShooting();
     Debug.DrawRay(shotOrigin.position,shotOrigin.forward,Color.red);
 }
 
 function IsShooting()
 {
     
     if(Input.GetAxisRaw("Triggers_360") && triggerDown == false)
     {
         
             if(Physics.Raycast(shotOrigin.position,shotOrigin.forward,hit,shotDistance))
             {
                 DrawHoleDecals();
                 
                 if(hit.collider.tag =="Enemy")
                 {
                    
                 }
 
             }
             triggerDown=true;
             
     }
     if(Input.GetAxisRaw("Triggers_360")==0)
     {
         triggerDown=false;
     }
 }
 
 function DrawHoleDecals()
 {
     
     for(i = 0; i < bulletHoleDecals.Length;i++)
     {
        // bulletHoleDecals[i]=
         
         if(!bulletHoleDecals[i]==false)
         {
             Debug.Log("gotHere");
 
             //Debug.Log("gotHere");
             
               if(Physics.Raycast(shotOrigin.position,shotOrigin.forward,hit,shotDistance))
               {       
                   bulletHoleDecals[Random.Range(0,3)].SetActive(true);
                   bulletHoleDecals[Random.Range(0,3)].transform.position=hit.point;
                       //(bulletHoleDecals[Random.Range(0,2)],hit.point,Quaternion.FromToRotation(Vector3.up,hit.normal));
                   bulletHoleDecals[Random.Range(0,3)].transform.rotation=Quaternion.FromToRotation(Vector3.up,hit.normal);
               }
               
               
             break;
              
         }
         
     }
    
    
 }

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 Davon92 · Mar 24, 2015 at 05:38 PM

thanks to NoseKills this is the new and improved script for anyone doing object pooling in unity's javascript along with a short example for raycasting and using decals to make anything from bullets to bloodsplatter appear in your own way the only thing this doesn't do is add a tiny position change to stop z fighting;

 var shotOrigin:Transform;
 var shotDistance:float;
 //var fireRate:float;
 var hit : RaycastHit;
 var bulletHoleDecals:GameObject[];
 var poolAmount:int;
 var triggerDown:boolean;
 var vex = new Array();
 
 function Start () 
 {
      //bulletHoleDecals = new Array();
      
     
     triggerDown=false; 
    
     for(var i = 0;i<poolAmount;i++)
     {
         var obj:GameObject=GameObject.Instantiate(bulletHoleDecals[Random.Range(0,3)]);
         obj.SetActive(false);
         vex.Add(obj);
     }
     
 }
 
 function Update () 
 {
     IsShooting();
     Debug.DrawRay(shotOrigin.position,shotOrigin.forward,Color.red);
 }
 
 function IsShooting()
 {
     
     if(Input.GetAxisRaw("Triggers_360") && triggerDown == false)
     {
         
             if(Physics.Raycast(shotOrigin.position,shotOrigin.forward,hit,shotDistance))
             {
                 DrawHoleDecals();
                 
                 if(hit.collider.tag =="Enemy")
                 {
                    
                 }
 
             }
             triggerDown=true;
             
     }
     if(Input.GetAxisRaw("Triggers_360")==0)
     {
         triggerDown=false;
     }
 }
 
 function DrawHoleDecals()
 {
     
     for(i = 0; i < poolAmount;i++)
     {  
         if(!vex[i].activeSelf)
         {
             
               if(Physics.Raycast(shotOrigin.position,shotOrigin.forward,hit,shotDistance))
               {       
                   
                   vex[i].transform.position=hit.point;
                      //this can be useful if you want to change which bullets are set active at a given time [Random.Range(0,2)] for var[i].SetActive(true);
                   vex[i].transform.rotation=Quaternion.FromToRotation(Vector3.up,hit.normal);
                   vex[i].SetActive(true);
                   break;
               }
               
         }
         
     }
    
    
 }


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 NoseKills · Mar 24, 2015 at 05:44 PM 0
Share

You should click the checkmark next to the answer to mark this thread as solved

avatar image Davon92 · Mar 24, 2015 at 05:58 PM 0
Share

well i still had the question about the decal position.

avatar image
0

Answer by NoseKills · Mar 24, 2015 at 02:23 PM

At least your DrawHoleDecals() has some strange stuff in it.

  if(!bulletHoleDecals[i]==false)
 

If you want to check which decal is not active yet (not used). use

 if (!bulletHitDecals[i].activeSelf)

Then just make sure you set them back to inactive or you'll run out of decals quickly or have to create a pool of million decals.

You have an unnecessary "var" in the Start() method.

  var bulletHoleDecals = new Array();

this makes it so that the array in Start() is a different one from the one you define in the class and use later in DrawHoleDecals . Make it just

  bulletHoleDecals = new GameObject[poolAmount];
  ....
  for(var i = 0;i<poolAmount;i++)
  {
      var obj:GameObject=GameObject.Instantiate(Decals[Random.Range(0,2)]);
      obj.SetActive(false);
      bulletHoleDecals[i] = obj;
  }

And here...

 bulletHoleDecals[Random.Range(0,3)].SetActive(true);
 bulletHoleDecals[Random.Range(0,3)].transform.position=hit.point;
  //(bulletHoleDecals[Random.Range(0,2)],hit.point,Quaternion.FromToRotation(Vector3.up,hit.normal));
  bulletHoleDecals[Random.Range(0,3)].transform.rotation=Quaternion.FromToRotation(Vector3.up,hit.normal);

you take a new random number for each property you change so you might set decal 0 active, move decal 1 to hit.point and rotate decal 2 to correct orientation.

 for(i = 0; i < bulletHoleDecals.Length;i++)
  {
     if(!bulletHoleDecals[i].activeSelf)
     {
        if(Physics.Raycast(shotOrigin.position,shotOrigin.forward,hit,shotDistance))
        {       
            bulletHoleDecals[i].SetActive(true);
            bulletHoleDecals[i].transform.position=hit.point;
            bulletHoleDecals[i].transform.rotation=Quaternion.FromToRotation(Vector3.up,hit.normal);
Comment
Add comment · Show 12 · 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 Davon92 · Mar 24, 2015 at 03:03 PM 0
Share

Thanks you nose kills for your advice. let me try to explain what i was doing and what i had changed.

$$anonymous$$y draw holes decal

 if(!bulletHoleDecals[i]==false)

was a test to see what would make the inactive clones turns on. but originally i had it set to

 if(!bulletHoleDecals[i].SetActiveInHeirarchy)

which again is suppose to look for any inactive decals and use those.

This is based off of the C# live demonstaration for Object Pooling on the Unity Website.

I've also since changed the BulletHoleDecals from a Random.Range back to just i

 bulletHoleDecals[i].SetActive(true); 
 
 bulletHoleDecals[i].transform.position=hit.point; 
 
 bulletHoleDecals[i].transform.rotation=Quaternion.FromToRotation(Vector3.up,hit.normal);

I want to randomize the decals for better visual feedback, i want it to feel like the bullets are hitting the object differently from each other.As far as how the decals are being Randomly instantiated at start it is fine and to my liking. The fact is that i cant seem to figure out how to call the clones that I instantiated in the start function from the Drawholes() Section.

avatar image Davon92 · Mar 24, 2015 at 03:19 PM 0
Share

The way i understand it.

$$anonymous$$y start function immediately sets my temp var obj which stores my decal instantiate to inactive.

$$anonymous$$y bulletHoleDecals.ADD is suppose to add my temp variable to my bulletHoleDecals Array for further use.

that is why i called bulletholedecals[i] it in the DrawHoles()

i don't know what the i is for other than a random value.

Also the bulletholesdecal setup in the start function cant leave the start function which is what i think my problem comes from.

I had to create a new var in the start function in order to make a new Array locking that new array to the start function only.

so I cant call it down in the DrawHoles() like i want to. i can see where it breaks down i just don't know what should be there.

avatar image NoseKills · Mar 24, 2015 at 03:20 PM 0
Share

Oh i just spotted you have an unnecessary "var" in the Start() method.

 var bulletHoleDecals = new Array();

this makes it so that the array in Start() is a different one from the one you define in the class and use later in DrawHoleDecals . $$anonymous$$ake it just

 bulletHoleDecals = new GameObject[x];

I'll modify my answer

avatar image Davon92 · Mar 24, 2015 at 03:23 PM 0
Share

im glad that i now know that that is the issue. but i cannot set it without the var i will get an error stating that i have

 a `$$anonymous$$issing$$anonymous$$ethodException: UnityEngine.GameObject[].Add
 Boo.Lang.Runtime.DynamicDispatching.$$anonymous$$ethodDispatcherFactory.ProduceExtensionDispatcher ()
 Boo.Lang.Runtime.DynamicDispatching.$$anonymous$$ethodDispatcherFactory.Create ()
 Boo.Lang.Runtime.RuntimeServices.DoCreate$$anonymous$$ethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args)
 Boo.Lang.Runtime.RuntimeServices.Create$$anonymous$$ethodDispatcher (System.Object target, System.String name, System.Object[] args)
 Boo.Lang.Runtime.RuntimeServices+c__AnonStorey15.<>m__9 ()
 Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.Dispatcher$$anonymous$$ey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
 UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
 GunShot.Start () (at Assets/Scripts/GunShot.js:24)
 `

and it wont produce any decals

avatar image NoseKills · Mar 24, 2015 at 03:32 PM 0
Share

sry i'm having trouble remembering / writing the JavaScript syntax. I'll keep modifying the answer as you find problems :D

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Getting raycast to fire at touch position but only when second finger is on screen 0 Answers

AI Evasion help 1 Answer

Ignore(absorb) Raycast on Canvas in mobile 1 Answer

Applying damage with Flamethrower-like weapon 1 Answer

Animation not being played! 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