Search Unity

Need help to change script

Discussion in 'Scripting' started by unity_nIf_q0ODo9qPcw, Apr 19, 2019.

  1. unity_nIf_q0ODo9qPcw

    unity_nIf_q0ODo9qPcw

    Joined:
    Apr 10, 2019
    Posts:
    2
    Hey guys, I need your help to change a couple of scripts that I have found through a youtube tutorial. So in this tutorial, the FPS collides with elements bringing the count to zero and a door gets destroyed.

    What I need is that instead of making elements disappear by entering their trigger zone, I would like to destroy them with a mouse click.

    The original video is here


    And the original scripts are here:
    https://github.com/UnityTutorialsHD/Unity-Tutorial-Assets/blob/master/ObjectsToCollect.cs
    https://github.com/UnityTutorialsHD/Unity-Tutorial-Assets/blob/master/CountObjects.cs

    I have tried to adapt the code of ObjectsToCollect to work for me...but I'm just a designer... I'm not into coding.

    With the changes I did what happens is that when I click an object, all the objects with the ObjectsToCollect script attached disappear and the door opens, lol! Instead of just one disappearing and the count goes -1


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class m_ObjectSounds
    5. {
    6.     public AudioClip ObjectCapture;
    7. }
    8.  
    9. public class ObjectsToCollect : MonoBehaviour
    10. {
    11.     public GameObject egg;
    12.     private Camera playerCam;
    13.     private Ray playerAim;
    14.     public float RayLength = 3f;
    15.     private AudioSource AS;
    16.     public m_ObjectSounds ObjectSounds = new m_ObjectSounds();
    17.     public static int objects = 0;
    18.     // Use this for initialization
    19.     void Awake()
    20.     {
    21.         objects++;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     //void OnTriggerEnter(Collider plyr)
    26.     //{
    27.    //    if (plyr.gameObject.tag == "MainCamera")
    28.   //          objects--;
    29.     //    gameObject.SetActive(false);
    30.    // }
    31.  
    32.     //
    33.  
    34.     void Start()
    35.     {
    36.         playerCam = Camera.main;
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.     Ray playerAim = playerCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    42.     RaycastHit hit;
    43.  
    44.     if (Physics.Raycast(playerAim, out hit, RayLength))
    45.     {
    46.         if (hit.collider.gameObject.GetComponent<AudioSource>())
    47.         {
    48.             AS = hit.collider.gameObject.GetComponent<AudioSource>();
    49.         }
    50.      
    51.             if (Input.GetMouseButtonDown(0) && hit.collider.gameObject.tag == "egg")
    52.             {
    53.                 objects--;
    54.                 gameObject.SetActive(false);
    55.                 {
    56.                     AS.clip = ObjectSounds.ObjectCapture;
    57.                     AS.Play();
    58.                 }
    59.             }
    60.         }
    61.     }
    62. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You should have a single GameObject that is issuing the raycast. If ObjectsToCollect is on various objects, then they are all sending the raycast separately. Move this logic somewhere else.

    As written, when you click the mouse, every object with this script is sending its own raycast. If the raycast then successfully hits any object in the scene with the "egg" title, then all of these objects SetActive(false) themselves.

    So take all this raycast logic, move it to another script that you only have 1 instance of, and SecActive(false) the GameObject associated with the collider the raycast hit instead of the object the script is attached to.
     
  3. unity_nIf_q0ODo9qPcw

    unity_nIf_q0ODo9qPcw

    Joined:
    Apr 10, 2019
    Posts:
    2
    I thought that by targeting the "egg" I was only acting on that one. How can I act on several and yet continue to decrease a global count?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I thought you wanted to act on just the one you clicked though. What are you trying to do to the other ones you didn't click?