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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UnityAds Stuck

Discussion in 'Scripting' started by Fersutagames_, Mar 24, 2016.

  1. Fersutagames_

    Fersutagames_

    Joined:
    May 2, 2013
    Posts:
    52
    Hi guys!

    been a long time since i've been on the forums! :(

    but i've run into a bit of trouble with UnityAds and implementing it..

    Here's the UnityAds script for the ads;
    Code (CSharp):
    1. using UnityEngine.Advertisements;
    2.  
    3. public class UnityAdsExample : MonoBehaviour
    4. {
    5.   public void ShowAd()
    6.   {
    7.     if (Advertisement.IsReady())
    8.     {
    9.       Advertisement.Show();
    10.     }
    11.   }
    12. }
    and the script im trying to put the above code in, but it continuously shoots me 19 errors; i don't know if it's due to my placement, but if someone could help out that'd be amazing! i'm still new to c# usually i use Js. i want the ad to show up when an animal eats the player (marked // Call dead when enemy kill 1)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Advertisements;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.  
    8.  
    9.     public enum TypeJump
    10.     {
    11.         NormalJump ,
    12.         DoubleJump
    13.     }
    14.  
    15.     public enum TypeController
    16.     {
    17.         Player,
    18.         AI
    19.     }
    20.      
    21.     public Animator animPlayer;
    22.     public float speedJump = 500.0f;
    23.     public TypeController typeController = TypeController.Player;
    24.     public GameObject spritePlayer;
    25.     public bool IsJump { get { return isJumping; } }
    26.     public ParticleSystem particleJumpHitGround;
    27.     public ParticleSystem particleFallWater;
    28.     public ParticleSystem particleBlood;
    29.  
    30.     public AudioSource eatedAudio;
    31.     public AudioSource fallWaterAudio;
    32.  
    33.     private GameController gameController;
    34.     private BoxCollider2D boxPlayer;
    35.     private AudioSource sound_FX;
    36.     private Rigidbody2D rid;
    37.  
    38.     private Vector2 posHeighest;
    39.     private Vector2 posReach;
    40.     private bool isJumping;                                            // This value to check when player in state jump or not
    41.     private bool isDead;
    42.  
    43.     private const float distanceJumpOneStep = GameController.DISTANCE_OBJ;
    44.     private const float distanceJumpTwoStep = GameController.DISTANCE_OBJ * 2;
    45.     private const float jumpHeight = 1.0f;
    46.  
    47.     void Awake()
    48.     {
    49.         gameController = FindObjectOfType<GameController>();
    50.         boxPlayer = GetComponent<BoxCollider2D> ();
    51.         sound_FX = GetComponent<AudioSource> ();
    52.         isDead = true;
    53.  
    54.     }
    55.  
    56.     private bool isTaping;
    57.     private float timeTap = 0.2f;
    58.     private float lastTap = 0.0f;
    59.  
    60.     void Update()
    61.     {
    62. #if UNITY_EDITOR
    63.         if(typeController == TypeController.Player && !isDead)
    64.             EditorJump();
    65. #endif
    66.  
    67.         if(typeController == TypeController.Player && !isDead) {
    68.             if(Input.GetKeyDown(KeyCode.K)) {
    69.                 if(!isTaping)  {
    70.                     isTaping = true;
    71.                     StartCoroutine (SingleTap ());
    72.                 }
    73.  
    74.                 if(Time.time - lastTap < timeTap){
    75.                     Debug.Log ("Double tap");
    76.                     isTaping = false;
    77.                 }
    78.  
    79.                 lastTap = Time.time;
    80.             }
    81.         }
    82.  
    83.  
    84.  
    85.     }
    86.  
    87.     IEnumerator SingleTap(){
    88.         yield return new WaitForSeconds (timeTap);
    89.         if(isTaping) {
    90.             Debug.Log ("Single tap");
    91.             isTaping = false;
    92.         }
    93.     }
    94.  
    95.     #region ControlEditorOrAdroid
    96. #if UNITY_EDITOR
    97.     void EditorJump()
    98.     {
    99.         if (Input.GetMouseButtonDown(0) & !isJumping )
    100.         {
    101.             gameController.AddScore(1);
    102.             Jump(TypeJump.NormalJump);
    103.         }
    104.         else if (Input.GetMouseButtonDown(1) & !isJumping)
    105.         {
    106.             gameController.AddScore(2);
    107.             Jump(TypeJump.DoubleJump);
    108.         }
    109.     }
    110. #endif
    111.  
    112.     #if UNITY_ANDROID
    113.     public void NormalJumpButton(){
    114.         if(!isJumping && !isDead)
    115.         {
    116.             gameController.AddScore(1);
    117.             Jump(TypeJump.NormalJump);
    118.         }
    119.     }
    120.  
    121.     public void DoubleJumpButton(){
    122.         if(!isJumping && !isDead)
    123.         {
    124.             gameController.AddScore(2);
    125.             Jump(TypeJump.DoubleJump);
    126.         }
    127.     }
    128.  
    129.     #endif
    130.     #endregion
    131.  
    132.     #region JumpProcess
    133.     public void Jump(TypeJump typeJump)
    134.     {
    135. //        animPlayer.SetBool("jump",true);
    136.  
    137.         // Play sound jump
    138.         if (sound_FX)
    139.             sound_FX.Play ();
    140.  
    141.         isJumping = true;
    142.         switch (typeJump)
    143.         {
    144.             case TypeJump.NormalJump:          
    145.                 StartCoroutine( JumpParabol(distanceJumpOneStep));
    146.                 break;
    147.             case TypeJump.DoubleJump:              
    148.                 StartCoroutine( JumpParabol(distanceJumpTwoStep));
    149.                 break;
    150.         }
    151.     }
    152.  
    153.     IEnumerator JumpParabol(float distanceJump)
    154.     {
    155.         spritePlayer.transform.localScale = new Vector3 (1, 1, 1);
    156.  
    157.         posHeighest = new Vector2(transform.position.x + distanceJump / 2, 0 + jumpHeight);
    158.         posReach = new Vector2(transform.position.x + distanceJump, 0);
    159.  
    160.         // Jump
    161.         while (transform.position.y < posHeighest.y - 0.1f)
    162.         {
    163.             transform.position = Vector2.Lerp(transform.position, posHeighest, Time.deltaTime * speedJump );
    164.             yield return null;
    165.         }
    166.  
    167.         // Fall
    168.         while(transform.position.y > 0.1f)
    169.         {
    170.             transform.position = Vector2.Lerp(transform.position, posReach, Time.deltaTime * speedJump );
    171.             yield return null;
    172.         }
    173.          
    174.         isJumping = false;
    175.  
    176.  
    177.  
    178.         transform.position = posReach;
    179. //        animPlayer.SetBool("jump", false);
    180.  
    181.         // Animation scale when hit platform when finish jump
    182.         spritePlayer.transform.localScale = new Vector3 (1, 0.7f, 1);
    183.  
    184.         if(particleJumpHitGround) {
    185.             particleJumpHitGround.Clear ();
    186.             particleJumpHitGround.Stop ();
    187.             particleJumpHitGround.Play ();
    188.         }
    189.  
    190.         while(spritePlayer.transform.localScale.y <0.99)
    191.         {
    192.             spritePlayer.transform.localScale = Vector3.Lerp (spritePlayer.transform.localScale, new Vector3 (1, 1, 1), Time.deltaTime*10.0f);
    193.             yield return null;
    194.         }
    195.         spritePlayer.transform.localScale = new Vector3 (1, 1, 1);
    196.     }
    197.     #endregion
    198.  
    199.     public void PlayerStopMove()
    200.     {
    201.         StopAllCoroutines();
    202.     }
    203.  
    204.     // Call dead when fall water
    205.     public void FallWater()
    206.     {
    207.         fallWaterAudio.Play ();
    208.         boxPlayer.enabled = false;
    209.         isDead = true;
    210.         animPlayer.SetTrigger("dead_fall_water");
    211.  
    212.         // Set particle
    213.         particleJumpHitGround.Pause ();
    214.         particleFallWater.Play();
    215.  
    216.         StartCoroutine(CallGameOver());
    217.     }
    218.  
    219.     // Call dead when enemy kill
    220.     public void EatPlayer(){
    221.         eatedAudio.Play ();
    222.         boxPlayer.enabled = false;
    223.         isDead = true;
    224.         animPlayer.SetTrigger ("dead_eat_by_enemy");
    225.         StartCoroutine (CallGameOver ());
    226.  
    227.     }
    228.  
    229.     // Call from animation particle blood play when play fall
    230.     public void BloodParticlePlay()
    231.     {
    232.         particleBlood.Play();
    233.     }
    234.  
    235.     // Call dead when enemy kill
    236.     public void WolfEatPlayer()
    237.     {
    238.         eatedAudio.Play ();
    239.         boxPlayer.enabled = false;
    240.         isDead = true;
    241.         animPlayer.SetTrigger("dead_eat_by_wolf");
    242.  
    243.         particleBlood.Play();
    244.  
    245.         StartCoroutine(CallGameOver());
    246.     }
    247.  
    248.     IEnumerator CallGameOver()
    249.     {
    250.         yield return new WaitForSeconds(2.0f);
    251.         gameController.ChangeStateGame(StateGame.GameOver);
    252.  
    253.         //animPlayer.SetTrigger("idle");
    254.         spritePlayer.SetActive(true);
    255.         gameObject.SetActive(false);
    256.     }
    257.  
    258.     // Reset player
    259.     public void Reset()
    260.     {
    261.         boxPlayer.enabled = true;
    262.         isDead = false;
    263.        spritePlayer.SetActive(true);
    264.  
    265.         print ("run");
    266.     }
    267.  
    268.     // Method to reset sprite layer
    269.     public void ResetSprite()
    270.     {
    271.         var sprites = transform.GetComponentsInChildren<SpriteRenderer>();
    272.  
    273.         foreach (SpriteRenderer spr in sprites)
    274.             spr.sortingLayerName = "Default";
    275.     }
    276.  
    277.  
    278.  
    279.  
    280.  
    281. }
    282.  
    If you can help out that'd be great! And if you're near where i live ill buy you a beer! :)
     
  2. Fersutagames_

    Fersutagames_

    Joined:
    May 2, 2013
    Posts:
    52
    Or am i going about it completely wrong?