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

Control current player not all of them.

Discussion in 'Scripting' started by JoePhil, Jun 10, 2015.

  1. JoePhil

    JoePhil

    Joined:
    May 12, 2015
    Posts:
    6
    I'm making moving platform that move back and forth until the player jumps on them. then they stop. This script works fine, except the fact that all the platforms are on the same prefab, so if one stops they all stop. and once i jump off they all move. How could i make it so only one stops when i jump on it without making multiple prefabs?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Post the code that makes them stop.
     
  3. JoePhil

    JoePhil

    Joined:
    May 12, 2015
    Posts:
    6
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Collide : MonoBehaviour {
    5.  
    6.     void OnCollisionEnter (Collision col)
    7.     {
    8.         if(col.gameObject.name == "Sphere")
    9.         {
    10.             Debug.Log("Hit");
    11.             growingFloor.onTop =false;
    12.         }
    13.     }
    14. }
    15.  
    if the OnTop statement is true they move, if it's false they dont.
     
  4. JakeBilbe

    JakeBilbe

    Joined:
    Jun 10, 2015
    Posts:
    57
    Try swapping out the

    Code (csharp):
    1. col.gameObject.name
    for
    Code (csharp):
    1. col.transform.tag
    I'm not 100% certain but it could be that it's grabbing all Objects with that Name, I know setting it to tag might not make a difference but the amount of times something this small has helped is strange. If not mind showing us the code for the platforms?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Let me guess: growingFloor is a class, onTop is a static variable within it?

    That's why it's affecting everything: your platforms all reference a single, global variable.

    You want to make that variable non-static, and use
    Code (csharp):
    1. col.gameObject.GetComponent<growingFloor>().onTop = false;
    This will make it only affect the one object that you've collided with.