Search Unity

Help! Unexplained loop!

Discussion in 'Scripting' started by olidadda, Dec 4, 2019.

  1. olidadda

    olidadda

    Joined:
    Jul 27, 2019
    Posts:
    37
    I'm trying really hard to get a grip on Unity and C#, I was using a tutorial example from Okita's book, and apparently the "new" keyword can't be used to create Monobehaviours: fair enough, I substituted it with
    MyThis mt = gameObject.AddComponent<MyThis>();
    but now when I press play (code is attached to main camera) it keeps looping the print commands (as if it were in the Update function, but it's not!)

    Can anyone help me get my head around this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MyThis : MonoBehaviour
    6. {
    7.     float MyFloat;
    8.  
    9.     public void AssignMyFloat(float f)
    10.     {
    11.         MyFloat = f;
    12.     }
    13.  
    14.     public void ShowMyFloat()
    15.     {
    16.         Debug.Log(MyFloat);
    17.     }
    18.     public void AssignThisMyFloat(float MyFloat)
    19.     {
    20.         this.MyFloat = MyFloat; //using the this keyword, we can be sure that the variable is assigned to a class variable,
    21.                                 // and not something only in the function.
    22.                                 // The this keyword in only necessary when the name of a parameter in a function matches the variable in a class
    23.                                 // where the function's parameter appears. ß
    24.                                 //Debug.Log(MyFloat);
    25.         print("I'm working");
    26.     }
    27.     //        class MyAwkwardClass
    28.     //        {
    29.     //                int MyBadlyNamedInt = 0;
    30.     //                void PoorlyNamedFunction ()
    31.     //                {
    32.     //                        Debug.Log (this.MyBadlyNamedInt);
    33.     //                        int MyBadlyNamedInt = 7;
    34.     //                        Debug.Log (this.MyBadlyNamedInt);
    35.     //                }
    36.     //
    37.     //        }
    38.  
    39.     // Use this for initialization
    40.     void Start()
    41.     {
    42.         MyThis mt = gameObject.AddComponent<MyThis>();
    43.         mt.AssignMyFloat(3.0f);
    44.         mt.ShowMyFloat();
    45.         mt.AssignThisMyFloat(5.0f);
    46.         mt.ShowMyFloat();
    47.     }
    48. }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Any new component that you create will automatically Awake, Start, and do all that other stuff that components ordinarily do (assuming the GameObject it's attached to is active).

    So when you create a new MyThis component, that new component will Start, which will create a new MyThis component, which will Start, which will create a new MyThis component, etc.
     
  3. olidadda

    olidadda

    Joined:
    Jul 27, 2019
    Posts:
    37

    Thank you so much for answering! So it seems this tutorial wasn't really very well done in that case.

    Does this mean that I should take the cut the contents of start and plop them in a different script and then call the class MyThis?
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I haven't seen the tutorial, but I imagine they put the code for creating the new object inside a function that doesn't run automatically every time an object of that type is created (or possibly added some sort of condition to it).

    I can't really tell you where to put the code, because I'm not sure what you're trying to accomplish with this. If your goal is just for it to run once when the program starts, you could put it in the Start function of some other MonoBehaviour (so that the class being created isn't the same as the class doing the creation).
     
  5. olidadda

    olidadda

    Joined:
    Jul 27, 2019
    Posts:
    37

    Ok, just tried making another script with the contents of the start function (after removing them from MyThis) and calling the MyThis class from there in the start function of the other script component.

    Thank you so much for your help, I wouldn't have understood otherwise!