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

How do I stop an immediate collision with all objects from ocuring at the entry of game mode?

Discussion in 'Scripting' started by ushouldhirejb, Jun 13, 2014.

  1. ushouldhirejb

    ushouldhirejb

    Joined:
    May 13, 2014
    Posts:
    36
    This is the simple script I'm running..

    var GUI : GUITextExample; var Player : Transform;

    function Start () {

    GUI = GameObject.FindWithTag("GUI").GetComponent(GUITextExample);

    }

    function OnCollisionEnter(collision : Collision) {

    if(collision.transform == Player); GUI.Counter += 1;

    if(collision.transform == Player); Destroy(gameObject, 2); }

    If I put out a bunch of objects, lets say 5 cubes with this script attached, as soon as I enter gamemode the counter will increase by 5 & 2 seconds later all of the cubes will be destroyed. If I turn off the destroy call then the counter still jumps to 5 but the script & counter work fine after that counting all of the player/cube collisions. Its like player throws out an immediate collision at start to everything and then the script & counter work fine? I tried moving this line..

    GUI = GameObject.FindWithTag("GUI").GetComponent(GUITextExample); under the OnCollisionEnter function but that didn't make a difference.

    I also tried this with no luck.

    var GUI : GUITextExample; var Player : Transform;

    function Start () {

    }

    function OnCollisionEnter(collision : Collision) {

    if(collision.transform == Player); GUI = GameObject.FindWithTag("GUI").GetComponent(GUITextExample); GUI.Counter += 1; }
     
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
  3. ushouldhirejb

    ushouldhirejb

    Joined:
    May 13, 2014
    Posts:
    36
    I did that & now the script compiles but no longer works. The character collides with some cubes,passes through others the counter doesn't count them and they no longer get destroyed?
     
  4. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
  5. ushouldhirejb

    ushouldhirejb

    Joined:
    May 13, 2014
    Posts:
    36

    Nevermind,I got the scripti working now after removing the semicolons & adding the player to the var in the inspector. Thanks for taking the time to answer my question! I watched some of those tutorial videos on scripting when I first downloaded unity & I had a hard time understanding parts of them. Since then i have created scripts following along with tutorials, picked apart a few scripts to frankinstien my own and now would probably be a good time to go back & watch those beginner tutorials on scripts I'm sure they will make alot more sense to me now & i'm sure i'll learn some NeedTo Know stuff to avoid problems like this one that can be easily fixed from happening again. thanks again!