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 Ajaxx84 · Sep 11, 2011 at 06:38 PM · texturearrayrandomtimer

Trying to switch Textures on object based on timer

Ok so I have done a complete revamp to the script including all of the changes made via suggestions in this question and another I have. The new and improved script is below. The only thing I think I need at this point to get it working is how do I call the textures in the array to replace First Texture, Second Texture, Third Texture, and Fourth Texture?

 var textureArray : Texture2D[];
 var damageAmount = int;
 var healAmount = int;
 var ammoAmount = int;
 var pointAmount = int;
 var damageDelay : float = 1;
 var healDelay : float = 1;
 var interval:float = 10; 
 var myTimer: float = 10;
 
 private var nextDamageAllowed : float;
 private var nextHealAllowed : float;
 private var nextAmmoAllowed : float;
 private var nextPointAllowed : float;
 
 function Update () 
 {
    if(myTimer > 0)
    {
       myTimer -= Time.deltaTime;
    }
    
    if(myTimer <= 0)
    {
       randomNumber = Random.Range(0, textureArray.length);
       renderer.material.mainTexture = textureArray[randomNumber];
       myTimer += interval;
    }
 }
 
 //When player enters trigger hurt them if the hurtTexture is active
 function OnTriggerEnter(thecollision : Collider)
 {
    if(theCollision.gameObject.tag == "Player" && First Texture)
    {
     GiveDamage;
    } else if (theCollision.gameObject.tag == "Player"  && Second Texture) {
       //when player enters trigger heal them if the healTexture is active
       GiveHeal;
    } else if (theCollision.gameObject.tag == "Player" && Third Texture) {
       //when player enters trigger give them ammo if the AmmoTexture is active
       GiveAmmo;
    } else if (theCollision.gameObject.tag == "Player" && Fourth Texture) {
       //when player enters trigger give them points if the PointTexture is active
       GivePoints;
    }
 }
 
 //when player stands on trigger hurt them periodically if the HurtTexture is active
  function OnTriggerStay(thecollision : Collider)
  {
     if (theCollision.gameObject.tag == "Player" && First Texture)
     {
        GivePeriodicDamage;
     } else if (theCollision.gameObject.tag == "Player"  && Second Texture) {
        //when player stands on trigger heal them periodically if the healTexture is active
        GivePeriodicHeal;
     } else if (theCollision.gameObject.tag == "Player" && Third Texture) {
       //when player stands on trigger give them ammo periodically if the AmmoTexture is active
       GivePeriodicAmmo;
     } else if (theCollision.gameObject.tag == "Player" && Fourth Texture) {
       //when player stands on trigger give them points periodically if the PointTexture is active
       GivePeriodicPoints;
   }
 }
 
 //Function to damage player for OnTriggerEnter
 function GiveDamage()
 {
    SendMessage("ApplyDamage", damageAmount, SendMessageOptions, DontRequireReceiver);
 }
 
 //Function to heal player for OnTriggerenter
 function GiveHeal()
 {
    SendMessage("Heal", healAmount, SendMessageOptions, DontRequireReceiver);
 }
 
 //Function to give ammo to player for OnTriggerEnter
 function GiveAmmo()
 {
    gunScript.clips += ammoAmount;
 }
 
 //Function to give points to player for OnTriggerEnter
 function GivePoints()
 {
    PointManager.points += pointAmount;
 }
 
 //Function to hurt player for OnTriggerstay
 function GivePeriodicDamage()
 {
   if(Time.time > nextDamageAllowed)
   {
       SendMessage("ApplyDamage", damageAmount, SendMessageOptions, DontRequireReceiver);
       nextDamageAllowed = Time.time + damageDelay;
    }
 }
 
 //Function to heal player for OnTriggerStay
 function GivePeriodicHeal()
 {
   if(Time.time > nextHealAllowed)
   {
      SendMessage("Heal", healAmount, SendMessageOptions, DontRequireReceiver);
      nextHealAllowed = Time.time + healDelay;
    }
 }
 
 //Function to give ammo to player for OnTriggerStay
 function GivePeriodicAmmo()
 {
   if(Time.time > nextAmmoAllowed)
   {
      gunScript.clips += ammoAmount;
      nextAmmoAllowed = Time.time + ammoDelay;
    }
 }
 
 //Function to give points to player for OnTriggerStay
 function GivePeriodicPoints()
 {
    if(Time.time > nextPointAllowed)
    {
      PointManager.points += pointAmount;
      nextPointAllowed = Time.time + pointDelay;
   }
 }
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 Ajaxx84 · Sep 12, 2011 at 12:10 AM 0
Share

Ok I have posted the full script that the original portion belongs to in the original post (for formating reasons). I need to know how to replace HurtTexture, HealTexture, AmmoTexture, and PointTexture with the textures in the array since I no longer have functions for them...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Piflik · Sep 11, 2011 at 09:53 PM

Well...you need to add 'break;' after each 'case' line, or it will just run through all 4 cases and display the last one. Also, the upper limit of Random.Range is excluded, so you will have to use a 5 there instead of 4.

Reset() would be just 'myTimer = 10.0' if I'm not missing something obvious. Wouldn't even need a function, you could add this line simply to your 'if(myTimer

A little addendum: personally I think FixedUpdate() is better suited for counters than Update().

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 Ajaxx84 · Sep 11, 2011 at 11:26 PM 0
Share

Thanks for the input I have edited the script quite a bit and got it working to a certain point. And just curious are you sure that the 4 won't count in random.range? currently the above script worked (to a point) but it only ever displayed the texture in case 4 spot. I have also used random 1,3 in another game that generates 3 types of loot with no problem.. did I accidently find some loophole in the other script or something?

Not saying you are wrong, don't misunderstand me, just trying to better understand the feature.

avatar image Piflik · Sep 12, 2011 at 09:51 AM 0
Share

Yeah, I am sure. Random.Range(1, 4) will only ever generate 1, 2 or 3, never 4. It is displaying material 4 in your original script, because you were missing the 'break;' after each 'case'. It actually displayed all of them in turn, but too fast for you to see it until it settled for the last one.

avatar image
0

Answer by Waz · Sep 11, 2011 at 08:57 PM

Random.Range(1,4) never returns 4.

switch cases should end in a break statement.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How can i delete an item from an array after being used? (So, it won't be repeated) 2 Answers

Changing Objects Material Using My Array 1 Answer

problem with FindWithTag 2 Answers

Random textures... 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