Search Unity

Question how do i made a script interact with another?

Discussion in '2D' started by unity_005946E12C5F7D78583B, Jun 3, 2023.

  1. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
    im making a platformer, one thing i want to do is for the last platform you stand on to shine, so i thought that i could do it by making a variable (lets called it isIn) that makes it shine, have a general script somewhere else, and when the player stands on a platform, the code of that platform tells the general script to set isIn to false in all instance of the platform and then, if that action gets called and after it, the platform where your standing on gets isIn to true, making it shine (im using a variable to do this because i want to do something else with shining platforms but i wont tell you about it since its important and a secret hehe)

    but i dont know how to make a script interact with another so i started to watch tutorials, but the thing is, im seriously confused about this, i legitematly dont understand it, maybe it is that they are working with prefabs and im not so maybe i should but idk why, or maybe it is that they are doing more complex stuff and i just want to set a variable to false in all instances of an object

    so, my question is, how do i make a script in a object interact wiht the script of another object? do I need prefabs? what is the most effective way to do it? the only thing that i got clearly is that some ways ar bad but the "good ways" were hard to understand, or maybe im just dumb and not ready for actual coding?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Start with tutorials. Nobody is going to custom-type one right here in a box for you. This is simple, simple simple basic stuff and covered well here:

    Learning

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    It isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

    Use this process when learning:

    Imphenzia: How Did I Learn To Make Games:

     
  3. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    create a reference to the 2nd script on the first script, then drag the 2nd script to the 1st script on the inspector