Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnTriggerEnter

Discussion in 'Scripting' started by NOIG, Oct 2, 2014.

  1. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    I need help with this OnTriggerEnter code, the console sayes
    Code (csharp):
    1.  A
    2. ssets/SpeedBuff.cs(8,30): error CS1624: The body of `SpeedBuff.OnTriggerEnter(UnityEngine.Collider)' cannot be an iterator block because `void' is not an iterator interface type
    3.  


    This is my code


    Code (csharp):
    1.  
    2.  
    3.        void OnTriggerEnter(Collider other) {
    4.        fowardSpeed = FSpeed;
    5.        FSpeed += 15;
    6.        sideStep = SSpeed;
    7.        SSpeed += 0.5;
    8.        backSpeed = BSpeed;
    9.        BSpeed += 0.5;
    10.        yield return new WaitForSeconds(250);
    11.        FSpeed -= 15;
    12.        BSpeed -= 0.5;
    13.        SSpeed -= 0.5;
    14.       }
    15.  
    16.  
    17.  
    18.  
     
    Last edited: Oct 2, 2014
  2. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    Any takers?
     
  3. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    Plz any one
     
  4. Chris11

    Chris11

    Joined:
    Sep 26, 2014
    Posts:
    9
    iam also new to c# but maybe we can figure it out togehter :D

    so iam confused by 2 things:

    no declaration for variables ?
    0.5 is usually float and needs to be written like this: 0.5f
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Code (csharp):
    1. yield return new WaitForSeconds(250);
    You can only use this in functions that return IEnumerator instead of void. You should have OnTriggerEnter run StartCoroutine(OtherFunction()).

    Example is in docs here:
    http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

    EDIT: Added 'f' to end of floats.
    Code (csharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.     StartCoroutine(OtherFunction());
    4. }
    5.  
    6. IEnumerator OtherFunction()
    7. {
    8.     fowardSpeed = FSpeed;
    9.     FSpeed += 15;
    10.     sideStep = SSpeed;
    11.     SSpeed += 0.5f;
    12.     backSpeed = BSpeed;
    13.     BSpeed += 0.5f;
    14.     yield return new WaitForSeconds(250);
    15.     FSpeed -= 15;
    16.     BSpeed -= 0.5f;
    17.     SSpeed -= 0.5f;
    18. }
     
    Last edited: Oct 2, 2014
  6. NOIG

    NOIG

    Joined:
    Jul 15, 2014
    Posts:
    32
    The variables where declared in a nother script, called in bye the
    Found here: http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
    Code (csharp):
    1.  
    2. Movement movement = GetComponent<Movement>();
    3. movement.EX();
    4.  
    And how I did it was call the object that the script was on
    EX
    Code (csharp):
    1.  
    2.  
    3. void Awake() {
    4. GameObject go = GameObject.Find("FatPlayer");
    5. //or
    6. GameObject players = GameObject.FindWithTag("Players");
    7. players.GetComponent<Movement>().EX();
    8. }
    9.  
    And I'm adding to a float so I don't need to put in the F
    EX
    Code (csharp):
    1.  public float Love = 69.6F;
    2. Love = L;
    3. L  += 5.6;
    4. //or
    5. public float Hi ;
    6. Hi += 0.2F;
    7.  
     
    Last edited: Oct 2, 2014
  7. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    If you don't put a 'f' or an 'F' then the decimal is treated as a double and there will be a compiler error. I updated my previous post.
    Code (csharp):
    1. //ERROR: 69.6 is a double, does not fit in a float
    2. //float Love = 69.6;
    3.  
    4. //Correct. F or f stands for float.
    5. float Love = 69.6F;
    6. Love = 69.6f;