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

Little help with my script

Discussion in 'Scripting' started by wolfer, Dec 9, 2015.

  1. wolfer

    wolfer

    Joined:
    Apr 10, 2013
    Posts:
    3
    Hi there I running into a problem with my script. So I'm having it where these objects I have with the name alien have it when they run into this wall they will destroy but for some reason they go to it but do not destroy. I have the right tags up. Here is the code for the wall I have

    function Update(){

    }

    function OnCollision(collider: Collision){
    if (collider.gameObject.tag == "alien"){
    Destroy(gameObject);
    }
    }



    and here is the code for the objects as alien that go to the wall

    var target : Transform;
    var moveSpeed = 3;
    var rotationSpeed = 3;
    var myTransform : Transform;

    function Awake()
    {
    myTransform = transform;
    }

    function Start () {
    target = GameObject.FindWithTag("robot").transform;
    }

    function Update () {
    transform.LookAt(Vector3(target.position.x, transform.position.y, target.position.z));

    myTransform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    }

    now I have it where the wall has robot and the alien have the tag alien on it. I will also say I tried using a ontrigger as well and trying a code on the aliens if they hit it they destroy. Anyone have any suggestions. I make it where its real simple just a square going to another cube.
     
  2. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
  3. wolfer

    wolfer

    Joined:
    Apr 10, 2013
    Posts:
    3
    I appreciate that but that is cs sharp. I'm using javascript at the moment. Plus there are some extra codes I don't need.
     
  4. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    The language doesn't matter, just put the code inside [ code][/code].

    By the way:
    To receive collision/trigger events a GameObject needs a Rigidbody. If you aren't using Physics just check the "Is Kinematic" checkbox.
     
  5. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    http://docs.unity3d.com/ScriptReference/Collision.html
    If you want something to occur on collision, you should use OnCollisionEnter(), there is no OnCollision().

    Edit: If you want others to look through your code, you should use the code tags as a common courtesy, no one wants to try understanding unformatted code.
     
    Last edited: Dec 9, 2015
  6. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
  7. wolfer

    wolfer

    Joined:
    Apr 10, 2013
    Posts:
    3
    you know what, I just see I didn't have the rigid body on with the kinematic on so let me see if that why it wasn't working. I really appreciate all this help.

    also sorry about that, I never posted in the forums before so my mistake. terrible sorry about that.
     
    Last edited: Dec 9, 2015
  8. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    What @flonch said is true, check it out.
     
  9. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    Going to lecture about common courtesy, then when giving a correction, don't assume anyone else knows it or knows how. Thats an awesome idea, having seen others use that, would like to, but I have no idea how, and I guess I could start searching, but the problem is I"m ALREADY searching for an answer for other things, and thats not working out so well.

    Wow, came off way angrier then I meant it. >_<

    But the concept remains, if your going to be condescending about it, at least have the good grace to tell how to do whatever it is you so feel is required.

    [-c-o-d-e-] [-/-c-o-d-e-] (take out the "-"'s)
     
    Last edited: Dec 10, 2015
  10. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    One sentence is a lecture? I was just reinforcing what ung was saying, since wolfer seemed to be coming up with reasons to not use code tags at the time of my post.
    Also look at post #2 and #4, I thought wolfer read those so why would I bother explaining it myself?
     
  11. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    YES, TOTALLY A LECTURE!

    No, not really, like I suspected then, I had no business posting a hello at that moment, let alone anything else. Everything I was typing was coming off angry.

    So, in conclusion, my apologies. =p