Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Hey Guys i need Your help at C# Coding!!! :(

Discussion in 'Scripting' started by EzraABRHM, Jun 14, 2018.

  1. EzraABRHM

    EzraABRHM

    Joined:
    Jun 14, 2018
    Posts:
    4
    I have to deactivate a turret when my character dies.

    Now I'm in the turret script and want to make the condition that if the curHealth of my character = 0, the turret will not fire anymore.

    I have the code so far, but how do I refer to my curHealth in the "Player" script and put it in my turret script? And how about my references?

    if (???????????????? curHealth <0)
    {
    turretAI.Attack (false);
    }

    Sorry for my bad english and I hope to get help :)

    Best greets

    Adrian
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,392
    That's not a problem. :)

    Have you been following a tutorial here? Getting access to other game objects is a very standard thing to do and there are various ways to do it. Pretty much all tutorials will cover these for you.

    For example, one way may be to use GetComponent. Another may be to serialize a reference within the Editor.

    If you haven't done so already, try clicking on "Learn" at the very top of this page. Lots of good stuff there. :)
     
    whileBreak likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,355
    You need to get a reference to the Player script. There's lots of ways to get references. Research setting references in the inspector, using GetComponent, using FindWithTag, using physics collisions to get a reference, etc.
     
    whileBreak and Doug_B like this.
  4. EzraABRHM

    EzraABRHM

    Joined:
    Jun 14, 2018
    Posts:
    4
    Thanks for the quick answers :)

    I learned to make an own small Video game since one week. I come from Germany and maybe sometimes i don't understand something because of my bad english :(

    So I watched some Tutorials and they all coding in there own style and for me this way is the good solution of the problem and i can go further to the GameOver UI :)

    This is the only thing i don't really understand.

    So the references like:

    public Player player;

    or as GameObject

    Gameobject Player ?

    and the second part like:

    if (player.GetComponent("Player").curHealth < 0);

    ??



    Thanks for your help guys :)
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,355
    As @Doug_B suggested, pretty much any intro to Unity tutorial will cover getting references to other scripts and other GameObjects, as it is something that is central to how Unity works. I'd just look for some other tutorials that cover it better than what you've already looked at.

    Also there's the manual:
    https://docs.unity3d.com/Manual/ControllingGameObjectsComponents.html
     
  6. EzraABRHM

    EzraABRHM

    Joined:
    Jun 14, 2018
    Posts:
    4
    Ok :) Thanks a lot :) i will look for it now :)
     
    Doug_B likes this.
  7. EzraABRHM

    EzraABRHM

    Joined:
    Jun 14, 2018
    Posts:
    4
    Sooooo now I have something but it won't really works :( Any Idea?

    In my TurretAI Script:



    public Player player;



    if (player.curHealth <= 0)
    {
    Destroy(GameObject.FindGameObjectWithTag("TurretRangeCollider"));
    }





    The Colliders are the range, so if i get into this colliders (on the left and right) the turret will shoot.

    But after my code the turret shoots again and again :(
     
  8. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,392
    There are a few problems here.
    1. There is not enough code shown to get a reasonable context of what is happening.
    2. It is not advisable to find game objects in that manner. You would tend to do that sort of thing in Awake() or Start().
    3. Further, if you are using colliders, you will want to use the dedicated methods to handle them (such as OnCollisionEnter).
    4. Try to avoid using public variables. Prefer instead to use protected/ private variables with the [SerializeField] attribute if you want to set them in the Editor.
    5. There is no code there to do with a turret shooting.
    Honestly, do give some tutorials a try. It is the fastest way to get started with this stuff. :)