Search Unity

Please help with this C# code

Discussion in 'Scripting' started by jkarateking, Mar 6, 2016.

  1. jkarateking

    jkarateking

    Joined:
    Dec 3, 2013
    Posts:
    127
    I am new to Unity and I am practising techniques. I code in C#.

    So basically, I am using the standard assets that are in the Unity Store. I only downloaded these for the first person controller.

    I know how to make it so if I click a wall in my game, the wall shoots in a certain direction. However, I would like it to work only if the first person controller is in a certain distance to the wall.

    The code I made was:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PhysicsPush : MonoBehaviour {
    5.  
    6.     public Object Player;
    7.     public Object Wall;
    8.  
    9.     public int force;
    10.  
    11.     void OnMouseDown()
    12.     {
    13.         float distance = Vector3.Distance (Player.transform.position - Wall.transform.position);
    14.         if (distance <= 2)
    15.         {
    16.             GetComponent<Rigidbody>().AddForce (transform.forward * force);
    17.             GetComponent<Rigidbody>().useGravity = true;
    18.         }
    19.            
    20.     }
    21. }
    22.    

    This doesn't work however. How would I allow it to work.

    Thanks for the help,
    Your friendly neighbourhood spiderman
     
  2. jkarateking

    jkarateking

    Joined:
    Dec 3, 2013
    Posts:
    127
    The errors I get are:


    Assets/My Files/Scripts/PhysicsPush.cs(13,59): error CS1061: Type `UnityEngine.Object' does not contain a definition for `transform' and no extension method `transform' of type `UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)


    and:

    Assets/My Files/Scripts/PhysicsPush.cs(13,42): error CS1501: No overload for method `Distance' takes `1' arguments
     
  3. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    Use GameObject instead of Object as the type for Wall and Player.
     
    jkarateking likes this.
  4. jkarateking

    jkarateking

    Joined:
    Dec 3, 2013
    Posts:
    127
    Thankyou, that fixes the first error but the second one still remains.

    That is:

    Assets/My Files/Scripts/PhysicsPush.cs(13,42): error CS1501: No overload for method `Distance' takes `1' arguments


    Thanks in advance
     
  5. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    replace that '-'(subtract/minus) with a ' , '(comma). The Vector.Distance does the math for you.
     
    jkarateking likes this.
  6. jkarateking

    jkarateking

    Joined:
    Dec 3, 2013
    Posts:
    127
    Thankyou, the code works now :D

    Do you mind if I ask one more question. The GameObject which I need to set as the wall , is the object that the script is attached to. How would I get the gameobject that the script is attached to without having to assign the object by dragging the object into the slot.

    Thanks again
     
  7. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    To make things a lot easier on you , this script (if many are running) are going to downgrade the performance of the game. I would suggest looking into 'physics.raycast'. In short, it fires an invisible ray, and grabs information if it hits an object. You can then modify the information as needbe (such as the addforce). Using a 'raycast' will make your script more modular and run a lot smoother (by only executing and checking distance ONLY when the mouse is clicked)
     
    jkarateking likes this.
  8. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Cast a ray from the object to the wall
     
    jkarateking likes this.
  9. jkarateking

    jkarateking

    Joined:
    Dec 3, 2013
    Posts:
    127
    Ok thankyou, I will look into how to do all that :D