Search Unity

messaging scripts on collided objects C#

Discussion in 'Scripting' started by Bazz_boyy, Feb 28, 2015.

  1. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    Howdy guys, this is my first unity post. Sorry for any poor conduct.

    I am having an issue here. I have 4 players and each has their own PlayerManager script (Player1,2,3,4). I am trying to create a script for an object that slows players within a radius around it when interacted with by anything. My problem is, I don't know how find these players and then send them messages that effects each of their movespeed. (I am working with a friend, and he has created the playermanager scripts). I have to store this player somehow and then access it's scripts... hmmm

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class PoisonHexagon : MonoBehaviour {
    6.  
    7.     public int slowRadius;
    8.     public List<string> playerTags = new List<string>();
    9.  
    10.  
    11.     void Start ()
    12.     {
    13.         playerTags.Add ("Player_01");
    14.         playerTags.Add ("Player_02");
    15.         playerTags.Add ("Player_03");
    16.         playerTags.Add ("Player_04");
    17.     }
    18.  
    19.  
    20.     void Update () { }
    21.  
    22.     void OnTriggerEnter()
    23.         //store all colliders within a radius around this object into an array...
    24.         {
    25.         Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, slowRadius);
    26.  
    27.         //for each collider in hitColliders...
    28.         foreach (Collider collider in hitColliders)
    29.         {
    30.             //for each string in playerTags
    31.             foreach (string player in playerTags)
    32.             {
    33.                 //If the colliders tag is equal to any of those four strings
    34.                 if (collider.tag == player)
    35.                 {
    36.                     //startcoroutine and slow that player
    37.                     StartCoroutine ("SlowPlayer(collider)");
    38.                 }
    39.          
    40.             }
    41.         }
    42.     }
    43.      //slows the player for 5 seconds...
    44.     IEnumerator SlowPlayer(GameObject _player)
    45.     {
    46.  
    47.         _player.GetComponent<Player1Manager> ();
    48.         return null;
    49.     }
    50. }
    I'm not going to post the player scripts. I just want to know what the method is for accessing the scripts of these colliders(player gameobjects) and if my method of filtering out anything that is not a player is making sense... Lets just pretend my player scripts all have a movement speed variable. How would I access them so I can divide them by two?

    Thanks in advance!
     
    Last edited: Feb 28, 2015
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    OnTriggerEnter has a parameter of type Collider. Documentation
    You usually use the passed argument to receive any reference and information of the object that has entered the trigger.
    For instance, you can check what's the objects tag or name and then e.g. get a component (such as the transform, a script component etc).
     
    Bazz_boyy likes this.
  3. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    ahh okay... so how do I tell my script to get the components of the players before the game even knows they've collided? (lets say they have a movement variable and I want to change that)
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I thought you wanted to use a trigger and change the speed as soon as you entered the trigger?
     
  5. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    yeah a trigger of an object that's separate from the player... so lets say I have a cloud of gas that randomly spawns, and it slows players who are within the clouds area of effect. I want the cloud script to be able to send a message to that players movement script to reduce the players speed for x amount of time. The players within the cloud have been stored in a Collider array, and so I need to now figure out how I send each individual player script this message to slow down... Sorry if I misunderstood your previous question :S.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well, either the cloud or the player should have a script on it in which you define the behaviour for entering that specific trigger.
    I'd recommend to add the script to the cloud, as you would only need to check for players and perhaps other NPC-like creatures. If you handled everything in the player's script you'd run through numerous tag/name checks which is not neccessary.

    Basically something like this in a script attached to the cloud:

    Code (CSharp):
    1. void OnTriggerEnter(Collider collider)
    2.     {
    3.         if (collider.tag == "Player")
    4.         {
    5.             YourPlayerScript script = collider.GetComponent<YourPlayerScript>();
    6.             if(script != null)
    7.             {
    8.                 script.CallTheSlowDownMethodHere();
    9.             }
    10.         }
    11.     }
    I hope there aren't any mistakes in it.

    What this does: As soon as an object with a rigidbody or characterController enters the cloud's trigger, it's going to check the tag. If it's equal to "Player" in this case, it will try to get the script which you need for the further process.
    YourPlayerScript is just a placeholder in this sample, you have to replace it with the script which handles the movement speed.
    'CallTheSlowDownMethodHere()' is also just a placeholder, you should replace it with the actual method that adjusts the movement speed of the player.
    If the object's tag doesn't match the String or collider.GetComponent<...>() returns 'null', nothing will happen.

    Of course you can add your array etc if you really need them for further actions, but this is just a basic example.
     
    Bazz_boyy likes this.
  7. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    oooh I see now! So that's how you can do it... Alright awesome. Thanks a lot dude, I really appreciate it :)