Search Unity

Creating a script that attaches 2 objects together.

Discussion in 'Editor & General Support' started by ASRIEL_msk, Aug 21, 2018.

  1. ASRIEL_msk

    ASRIEL_msk

    Joined:
    Aug 21, 2018
    Posts:
    3
    I'm trying to create a physics-based destruction game, and in order to do so, I needed to create a script that would auto-connect the building parts to other rigid bodies upon spawning. Here's the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BuildingJoint : MonoBehaviour
    6.     {
    7.     // Use this for initialization
    8.     void Start()
    9.         {
    10.        function OnCollisionEnter (collision = Collision);
    11.             //if the collision is with a rigidbody
    12.             if (collision.rigidbody)
    13.             {
    14.                 //add a HingeJoint component to the gameobject that has the script
    15.                 gameObject.AddComponent(FixedJoint, breakForce: 20, breakTorque: 20)
    16.  
    17.                 //set the rigidbody that we collided with as the connected body the hinge connects to
    18.                fixedJoint.connectedBody = collision.rigidbody;
    19.  
    20.             }
    21.         }
    22.     }
    23.  
    24.  
    However there are some errors occurring within this script:
    *Assets/Scenes/Building joint.cs(10,34): error CS1525: Unexpected symbol `(', expecting `,', `;', or `='
    *Assets/Scenes/Building joint.cs(18,16): error CS1525: Unexpected symbol `fixedJoint'

    I wonder what's wrong with the script itself? I used a "sticky bomb" script as the base set for this script and had modified it so that a fixed joint is created with breakForce and breakTorque, but have I've formatted these correctly?
     
    Last edited: Aug 22, 2018
  2. ASRIEL_msk

    ASRIEL_msk

    Joined:
    Aug 21, 2018
    Posts:
    3
    This is important, as it can help with building large structures that react to physics faster.
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    You're missing a semicolon on line 15.
     
  4. ASRIEL_msk

    ASRIEL_msk

    Joined:
    Aug 21, 2018
    Posts:
    3
    Yes, the second error has been resolved, but the first one is still there.
     
  5. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Now that I look at it for real, you have quite a nonfunctional script on your hands. Looks like you took a UnityScript script and tried to paste it directly into C#. It's important to understand that those are two separate languages, and you can't just use one in the other. UnityScript is on its way out, having been deprecated, so you're going to want to stick to C#.

    Here's a cleaned up code snippet, but I would suggest that you take a look at some of Unity's tutorials on C# to familiarize yourself with the language a bit more as well.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class BuildingJoint : MonoBehaviour {
    5.     void OnCollisionEnter ( Collision collision ) {
    6.         //if the collision is with a rigidbody
    7.         if ( collision.rigidbody != null ) {
    8.             //add a HingeJoint component to the gameobject that has the script
    9.             FixedJoint fixedJoint = gameObject.AddComponent<FixedJoint>();
    10.             fixedJoint.breakForce = 20;
    11.             fixedJoint.breakTorque = 20;
    12.  
    13.             //set the rigidbody that we collided with as the connected body the hinge connects to
    14.             fixedJoint.connectedBody = collision.rigidbody;
    15.         }
    16.     }
    17. }