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

can someone say me is what's wrong with my script

Discussion in 'Scripting' started by Alfredo0075, Sep 22, 2016.

?

I don't know what's wrong, but I can not put this script in GameObjects because of an error

  1. help meeee

    0 vote(s)
    0.0%
  2. every thing

    2 vote(s)
    100.0%
  1. Alfredo0075

    Alfredo0075

    Joined:
    Sep 22, 2016
    Posts:
    1
    firt script :

    using UnityEngine;
    using System.Collections;

    public class ataques : MonoBehaviour {
    public Transform player;
    public bool direita;
    public bool esquerda;
    public bool defesa0;
    // Use this for initialization
    void Start () {




    }

    // Update is called once per frame
    void Update () {
    }
    void OnCollisionTrigger (Collision2D colisor){
    if (colisor.gameObject.name == "sword")
    defesa0 = true;
    else
    defesa0 = false;

    defesa0 = GetComponent<atack2>().defesa1;

    }

    }

    secund script:

    using UnityEngine;
    using System.Collections;

    public class atack2 : MonoBehaviour {
    public GameObject defender;
    public bool defesa1;

    // Use this for initialization
    void Start () {



    }

    void Update () {




    }
    }
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    Use code tags so we can read it.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Use code tags. Don't do a survey that serves no purpose. Post your error.

    But to add
    defesa0 = GetComponent<atack2>().defesa1; always happens on a collision, even if your if is true. But from a glance, defesa1 has no value.
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    is the error because there is a function inside the update function?

    Code (CSharp):
    1. // Update is called once per frame
    2. void Update () {
    3. }
    4. void OnCollisionTrigger (Collision2D colisor){
    5. if (colisor.gameObject.name == "sword")
    6. defesa0 = true;
    7. else
    8. defesa0 = false;
    9.  
    10. defesa0 = GetComponent<atack2>().defesa1;
    11.  
    12. }
    13.  
    14. }
    it should be like this
    Code (CSharp):
    1. // Update is called once per frame
    2. void Update () {
    3. }
    4.  
    5. }
    6.  
    7. void OnCollisionTrigger (Collision2D colisor){
    8. if (colisor.gameObject.name == "sword")
    9. defesa0 = true;
    10. else
    11. defesa0 = false;
    12.  
    13. defesa0 = GetComponent<atack2>().defesa1;
    14.  
    15. }
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Function isn't within the update. There is a opening and closing bracket for the update. It's just an empty update. The spacing does throw it off though, so that is easy to confuse (I thought that at first as well). Changing it to what you did would try to attach two closing brackets to update.
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    ok, no problem.
    Can you tell us what error you are getting from unity?
    Is the error msg happening when you run the game or unity doesn't let you run the game because their is an error?
    that info would help us a bunch.
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    is this line of code causing the error?

    defesa0 = GetComponent<atack2>().defesa1;

    in atack2 is defesa1 not set?
    in the the start() funtion add defesa1 = true;