Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Hint Reward from array after watching ad

Discussion in 'Scripting' started by Kiesco91, Jun 21, 2020.

  1. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    hey, so my project is an escape room and I want to offer a random clue from an array but you have to watch an ad first and then once you close the hint, it deletes it from the array. the ads script is working fine but I'm not sure how to add the array reward. ill post the important bits of the script below that i'm stuck with.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5.  
    6. public class HintsAds : MonoBehaviour, IUnityAdsListener
    7. {
    8.  
    9.     public GameObject[] hints;
    10.  
    11.  
    12.  
    13.  
    14.  
    15.  
    16.  
    17.  
    18. public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    19.     {
    20.         // Define conditional logic for each ad completion status:
    21.         if (showResult == ShowResult.Finished)
    22.         {
    23.             Debug.LogWarning("You Get A Hint.");
    24.  
    25.             ShowHint();
    26.         }
    27.  
    28.         else if (showResult == ShowResult.Skipped)
    29.         {
    30.             Debug.LogWarning("You DONT Get A Hint.");
    31.         }
    32.  
    33.         else if (showResult == ShowResult.Failed)
    34.         {
    35.             Debug.LogWarning("The ad did not finish due to an error.");
    36.         }
    37.     }
    38.  
    39.  
    40.  
    41.  
    42. public void ShowHint()
    43.     {
    44.         //show random hint from list of arrays
    45.     }
    46.  
    47.     public void CloseHint()
    48.     {
    49.         //button OnClick to delete Hint from array.
    50.     }
    51. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Arrays are immutable in length. You can change the elements, just not how many of them there are.

    At start you could copy the hints into a
    List<GameObject>
    instead. A List is mutable, as in you can call
    .Remove()
    on it and remove something.

    Alternatively, if you're on a recent version of Unity you can just use the list object directly as your public inspector item.

    Also you might want to check .Count (or .Length in the case of an array) before you offer the hint button in order to make sure there is at least one hint or else you won't have one to show the user.

    Code (csharp):
    1. GameObject[] myArray;
    2. List<GameObject> myList;
    3.  
    4. // to make a list out of the above at runtime:
    5.  
    6. myList = new List<GameObject>( myArray);
    7.  
    8. // to make an array out of the list at runtime:
    9.  
    10. myArray = myList.ToArray();
    It's not normally necessary to go back and forth, unless an API requires it. Stick with one or the other. If you need to add/remove items, use a List.
     
  3. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Hello! thank you for your response.

    I haven't had much time this week to into this. I wanted to use a List for this but I'm still struggling to implement it and get it working. I have gone with an array and instantiating the object but this isn't working and my UI just comes up as a big red X so i'm thinking of maybe just set.active instead but I can't find much online in the way of setting active array objects individually.

    This is where I'm at.

    Code (CSharp):
    1. public void ShowHint()
    2.     {
    3.         //show random hint from list of arrays
    4.         int randomIndex = Random.Range(0, hints.Length);
    5.  
    6.         GameObject clone = Instantiate(hints[randomIndex], transform.position, Quaternion.identity);
    7.     }
    this will spawn in a hint but it doesnt work. any thoughts?

    thanks in advance
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Any object that you want to go into a UI hierarchy (ie., under a Canvas) needs to be both Instantiated AND parented.

    You can do this as two separate steps but it offers many different ways of getting it wrong.

    Instead, use the overload of Instantiate() that takes a second argument that is a Transform and it will parent the new copy to that Transform.
     
  5. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Thank you,

    You are being such a big help! I just have one more question.

    I know its a random.Range thing I've using but I would like it to cycle through all the hints before repeating any of them.

    Code (CSharp):
    1.     IEnumerator showHint()
    2.     {
    3.  
    4.         //show random hint from list of arrays
    5.  
    6.         int randomIndex = Random.Range(0, hints.Length);
    7.  
    8.         GameObject newhint = Instantiate(hints[randomIndex], transform.position, transform.rotation) as GameObject;
    9.         newhint.transform.SetParent(GameObject.FindGameObjectWithTag("Parent").transform, false);
    10.  
    11.         yield return new WaitForSeconds(5);
    12.  
    13.         Destroy(newhint.gameObject);
    14.     }
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Instead of choosing a hint randomly, you can cycle through them with a counter that you store persistently.

    In this context "persistently" means with a lifecycle long enough to serve your purposes, which means different things if you intend it to persist across launches of the app (use PlayerPrefs) or just in a single session (just put it in a variable that does not get reset between hint showings).

    If you want the order to be random, shuffle it first only once, then step through them linearly as above.
     
  7. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Perfect! thank you sir! you have been amazing.

    I have got it all up and running.

    have a good day!
     
    GameLover90 likes this.