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. Dismiss Notice

Question Script in a cloned object affects other cloned objects with that script?

Discussion in 'Editor & General Support' started by OpenWorldCoder543, Sep 15, 2023.

  1. OpenWorldCoder543

    OpenWorldCoder543

    Joined:
    Dec 23, 2022
    Posts:
    4
    I'm making a procedural generation system, and I've made it clone and then place trees randomly. I'm now making it so you can break/mine these trees, but whenever I mine one tree, it mines them all. I think this is because the script sets the object to mine as EVERY cloned object. The script is in the attached files. The circled part is where it sets the object 'health' script.

    (The object health is an int btw in case that helps)


    Thanks in advance,

    OpenWorldCoder543

    :)
     

    Attached Files:

  2. Gorki1337

    Gorki1337

    Joined:
    Mar 2, 2018
    Posts:
    31
    Hi,
    Try using ScreenPointToRay instead of ViewportPointToRay if you are making 3D game because of specific interaction requirements in the 3D world.

    Example:
    Code (CSharp):
    1. Vector3 screenPos = Input.mousePosition;
    2. Ray ray = Camera.main.ScreenPointToRay(screenPos);
    3.  
    4. if (Physics.Raycast(ray, out RaycastHit hitInfo)) {
    5.     // do somehting
    6. }
    One more tip:
    Never use Camera.main except in Start or Awake method.

    Because Camera.main is this:
    GameObject.FindGameObjectWithTag("Camera").GetComponent<Camera>();
    just short version.
     
    Last edited: Sep 15, 2023
  3. OpenWorldCoder543

    OpenWorldCoder543

    Joined:
    Dec 23, 2022
    Posts:
    4
    Hi,

    I'm afraid this didn't work, resulting in the same effect. I'll show you the properties of the tree I'm cloning, if that helps in any way. (btw nothing changes in the cloned object, they're all the same except for the changes that the script made.)

    Thanks,
    OpenWorldCoder543
     

    Attached Files:

    • yes.png
      yes.png
      File size:
      60.5 KB
      Views:
      13
  4. Gorki1337

    Gorki1337

    Joined:
    Mar 2, 2018
    Posts:
    31
    Ohhh I understand the problem, thanks for the image.
    Move the MiningManager script from the tree to the camera and I think that will solve your problem. Because if that script is in a tree and you have like 50 trees, then the raycast will start for all 50 trees and subtract health.

    After moving the script to the camera, it will run only once, not 50 times, and it will only take the one tree you are looking at, not every tree.

    So remove script from Tree prefab and put it in Camera
     
  5. OpenWorldCoder543

    OpenWorldCoder543

    Joined:
    Dec 23, 2022
    Posts:
    4
    Thank you so much! You're an absolute LIFESAVER!
    I got it to work, thanks to you!
    It works perfectly!

    Thank you SO MUCH,

    OpenWorldCoder543!!!

    (thank you so much, you came in clutch :) )
     
  6. Gorki1337

    Gorki1337

    Joined:
    Mar 2, 2018
    Posts:
    31