Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Calling a function on multiple of the same script?

Discussion in 'Scripting' started by Spiv5, Apr 28, 2017.

  1. Spiv5

    Spiv5

    Joined:
    Aug 7, 2016
    Posts:
    8
    I currently have on function called respawnPlayer that respawns the player at the nearest checkpoint when he dies. I also want some falling platforms to reset when he does so, so I used fallingPlatform.reset() to do it, but the issue was only one of the game objects followed this behavior, all of the others ignored it. I now have all of the game objects that need to do so as an array, but it isnt working. Here is both scripts

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelManager : MonoBehaviour
    6. {
    7.  
    8.     public GameObject currentCheckpoint;
    9.  
    10.     private FallingPlatform fallingPlatform;
    11.  
    12.     private playerController player;
    13.  
    14.     public GameObject[] fp;
    15.     // Use this for initialization
    16.     void Start () {
    17.         player = FindObjectOfType<playerController>();  
    18.         fp = GameObject.FindGameObjectsWithTag("Fall");
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.        
    24.     }
    25.  
    26.     public void respawnPlayer()
    27.     {
    28.         Debug.Log("Player Respawn");
    29.         player.transform.position = currentCheckpoint.transform.position;
    30.         foreach (GameObject falling in fp)
    31.         {
    32.             fallingPlatform.reset();
    33.         }
    34.  
    35.     }
    36.  
    37. }
    38.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FallingPlatform : MonoBehaviour {
    6.  
    7.     private Rigidbody2D rb2d;
    8.     private Vector2 startPos;
    9.     private Quaternion startRot;
    10.  
    11.     public float fallDelay;
    12.  
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         rb2d = GetComponent<Rigidbody2D>();
    18.         startPos = transform.position;
    19.         startRot = transform.rotation;
    20.  
    21.     }
    22.  
    23.  
    24.  
    25.     void OnCollisionEnter2D(Collision2D col)
    26.     {
    27.         if(col.collider.CompareTag("Player"))
    28.         {
    29.             StartCoroutine(Fall());
    30.         }
    31.     }
    32.  
    33.     IEnumerator Fall()
    34.     {
    35.         yield return new WaitForSeconds(fallDelay);
    36.         rb2d.isKinematic = false;
    37.  
    38.         yield return 0;
    39.     }
    40.  
    41.     public void reset()
    42.     {
    43.         GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
    44.         this.transform.position = startPos;
    45.         this.transform.rotation = startRot;      
    46.         print(gameObject.name);
    47.     }
    48.  
    49. }
    50.  
    51.  
    Thanks in advance!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I copied this from your first script:
    Code (CSharp):
    1.  Debug.Log("Player Respawn");
    2.         player.transform.position = currentCheckpoint.transform.position;
    3.         foreach (GameObject falling in fp)
    4.         {
    5.             fallingPlatform.reset(); // This should be falling.reset();
    6.         }
    And made a comment in there. :) You had the right idea, but used the wrong variable ;)
     
  3. Spiv5

    Spiv5

    Joined:
    Aug 7, 2016
    Posts:
    8
    Of course, how didnt I realise! I knew it was going to be some stupid mistake. Thanks for the help!
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    smiles. np.