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

I NEED HELP! calling a function from another script but deleting it afterwards!

Discussion in 'Scripting' started by abdelrahmanyoussry509, Apr 23, 2020.

  1. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    hello i am new to unity and this fourm so if you undesrtand my problem please explain it to me as simple as possible

    problem is in line 70 in motion script and 77

    when i instantiate the object everything works fine but if i dont instantiate the object it gives me error since it can not find the refrence for the script
    can you tell me how to fix this and explain it to me as simple as possible for some one with low iq :p


    motion script!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Motion : MonoBehaviour
    6. {
    7.     public float speed;
    8.     private Rigidbody rig;
    9.     public float sprintmodifire;
    10.     public Camera PlayerCam;
    11.  
    12.     public Transform groundchek;
    13.     public LayerMask groundmask;
    14.     private float t_adjustedSpeed;
    15.  
    16.     public float jumpforce;
    17.     private float BaseFOV;
    18.     private float FOVmodifre = 1.2f;
    19.     void Start()
    20.     {
    21.         rig = GetComponent<Rigidbody>();
    22.         BaseFOV = PlayerCam.fieldOfView;
    23.     }
    24.  
    25.     public IEnumerator GrapplePush()
    26.     {
    27.         rig.AddRelativeForce((Vector3.forward * t_adjustedSpeed / 7));
    28.         yield return new WaitForSeconds(1f);
    29.         FindObjectOfType<GrapplingGunScript>().isGrapling = false;
    30.  
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void FixedUpdate()
    35.     {
    36.         bool isGorunded;
    37.         isGorunded = Physics.Raycast(groundchek.position, Vector3.down, 0.1f, groundmask);
    38.         //Axis and motion
    39.         float t_Hmove = Input.GetAxis("Horizontal");
    40.         float t_Vmove = Input.GetAxis("Vertical");
    41.         //we save them both in a vector 3
    42.         Vector3 t_direction = new Vector3(  t_Hmove,0, t_Vmove);
    43.         // when we press w and d at same time we add 1from w on 1 from d so it makes player go faster so we nomlize (keep it 1)
    44.         t_direction.Normalize();
    45.      
    46.         //Controls
    47.         bool spirnt = Input.GetKey(KeyCode.LeftShift);
    48.         bool jump = Input.GetKey(KeyCode.Space);
    49.  
    50.         //motion and sprint and camera lerping
    51.  
    52.         bool isjumping = jump;
    53.         bool isSprinting = spirnt && t_Vmove >0 && !isjumping && isGorunded;
    54.  
    55.         if (isjumping && isGorunded )
    56.         {
    57.             rig.AddForce( Vector3.up * jumpforce);
    58.         }
    59.          t_adjustedSpeed = speed;
    60.      
    61.  
    62.  
    63.      
    64.  
    65.  
    66.         if (isSprinting)
    67.         {
    68.             t_adjustedSpeed = sprintmodifire * t_adjustedSpeed;
    69.         }
    70.         if (!FindObjectOfType<GrapplingGunScript>().isGrapling || )
    71.         {
    72.             Vector3 TargetVelocity = transform.TransformDirection(t_direction) * t_adjustedSpeed * Time.deltaTime;
    73.             TargetVelocity.y = rig.velocity.y;
    74.             rig.velocity = TargetVelocity;
    75.             print("first work");
    76.         }
    77.         else if(FindObjectOfType<GrapplingGunScript>().isGrapling)
    78.         {
    79.  
    80.             StartCoroutine(GrapplePush());
    81.          
    82.             print("secounds work");
    83.         }
    84.      
    85.      
    86.         if (isSprinting)
    87.         {
    88.             PlayerCam.fieldOfView = Mathf.Lerp(PlayerCam.fieldOfView,BaseFOV *FOVmodifre,Time.deltaTime*8f);
    89.         }
    90.         else
    91.         {
    92.             PlayerCam.fieldOfView = Mathf.Lerp(PlayerCam.fieldOfView, BaseFOV , Time.deltaTime * 8f);
    93.         }
    94.  
    95.      
    96.      
    97.     }
    98.  
    99.  
    100. }
    101.  
    grapple gun script!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GrapplingGunScript : MonoBehaviour
    6. {
    7.     private LineRenderer Lr;
    8.     private Vector3 GrapplePoint;
    9.     public LayerMask Graprable;
    10.     public Transform GunTip;
    11.     private float MaxDistance =100f;
    12.     private SpringJoint Joints;
    13.     public bool isGrapling = false;
    14.  
    15.  
    16.     private void Awake()
    17.     {
    18.         Lr = GetComponent<LineRenderer>();
    19.     }
    20.  
    21.     private void Update()
    22.     {
    23.         //rather than use (public transfrom) we used this becuse in a prefab we can not refrence them when we instaciate
    24.         //so we refrence them using tags
    25.         var cam = GameObject.FindWithTag("Cam");
    26.         var Player = GameObject.FindWithTag("Player");
    27.  
    28.  
    29.         if (Input.GetMouseButtonDown(0))
    30.         {
    31.             StartGrapple();
    32.          
    33.          
    34.         }else if (Input.GetMouseButtonUp(0))
    35.         {
    36.  
    37.         }
    38.         void StartGrapple()
    39.         {
    40.             RaycastHit Hit;
    41.             isGrapling = true;
    42.             if (Physics.Raycast(origin: cam.transform.position,direction: cam.transform.forward,out Hit,MaxDistance,Graprable))
    43.             {
    44.                 GrapplePoint = Hit.point;
    45.                 Joints = Player.gameObject.AddComponent<SpringJoint>();
    46.                 Joints.autoConfigureConnectedAnchor = false;
    47.                 Joints.connectedAnchor = GrapplePoint;
    48.  
    49.                 float DistanceFromPoint = Vector3.Distance(a: Player.transform.position, b: GrapplePoint);
    50.  
    51.                 //distance it will try to keep you from grappling
    52.                 Joints.maxDistance = DistanceFromPoint * 0.8f;
    53.                 Joints.minDistance = DistanceFromPoint * 0.25f;
    54.  
    55.                 //change values how you see fit
    56.                 Joints.spring = 4.5f;
    57.                 Joints.damper = 7f;
    58.                 Joints.massScale = 4.5f;
    59.             }
    60.         }
    61.     }
    62.     private void LateUpdate()
    63.     {
    64.         DrawRope();
    65.     }
    66.     void DrawRope()
    67.     {
    68.         Lr.SetPosition(index: 0, GunTip.position);
    69.         Lr.SetPosition(index: 1, GrapplePoint);
    70.     }
    71. }
    72.  
     
    Last edited: Apr 23, 2020
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    You need to do a null check. For example:
    Code (csharp):
    1. var grappler = FindObjectOfType<GrapplingGunScript>();
    2. if (grappler != null && grappler.isGrapling) {
    There's also a shortcut called the "null coalescing" operator. Basically it means, "If this thing is null, return fallse, if it's not null, keep digging". It's the question mark in the following example:
    Code (csharp):
    1. if (FindObjectOfType<GrapplingGunScript>()?.isGrapling) {
     
    matkoniecz likes this.
  3. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    THANK YOU
    i thought about it being something to do with null thingy but i did not know how to use it properly XD
    thanks