Search Unity

Different instances of the same script

Discussion in 'Scripting' started by qvbreugel, Nov 13, 2017.

  1. qvbreugel

    qvbreugel

    Joined:
    Nov 13, 2017
    Posts:
    3
    Hey there,

    I've just started learning Unity and the C# language. Currently I'm working on my first mini-project. Unfortunately, I'm stuck already. In a simplified version of my project, I have two scripts and one game object. The game object is called "Timer", one script is called "CreateTimer" and the other is called "Countdown". In CreateTimer, I want to, naturally, create a timer. The current way I have this set up, is by having a function in CreateTimer that adds the Countdown component to Timer. My Countdown script basically exists out of a title and duration variable. How can I create a new Countdown with different values for name and duration while still being able to access them. For example to show two Countdown's titles and remaining times on the screen.

    I feel like this is very basic stuff, but I think I don't know the tools yet which can help me to accomplish this.Up until now, I've watched all basic scripting videos and I've done two introductory tutorials. Do you recommend any specific tutorials or videos to watch?

    Thanks in advance,

    Quinten
     
  2. MaxGuernseyIII

    MaxGuernseyIII

    Joined:
    Aug 23, 2015
    Posts:
    315
    I'm going to start by parroting your question back to you because I'm not certain I understand it.

    You have two scripts and a game object. One of the scripts is for modeling a timer. The other is for initiating timers. You want to know how to have the script that initiates timers be able to create more than one timer at a time.

    Is that correct?

    If so, I would start by looking at prefabs - which takes the idea of the Prototype pattern and makes it a first-class concept in Unity.

    Essentially, instead of trying to put all your timers on the same game object, have a prefab that represents an uninitiated timer. Every time you want to start a new timer, instantiate that prefab (which means, more or less, "make an exact copy that you can use"), then do the logic to initiate the newly-instantiated game object.

    If I didn't get it right, please give us a little more info to help square up our understanding of your problem.
     
  3. qvbreugel

    qvbreugel

    Joined:
    Nov 13, 2017
    Posts:
    3
    Yes, that is correct. I think I've dealt with prefabs in one of the tutorials, but forgot about them. I think they might be a good solution to this. Thank you very much! I'll go give it a shot.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can attach them to different objects and still keep references to them.
    Code (csharp):
    1. private Countdown someTimer;
    2. void Start() {
    3. GameObject spawned = new GameObject("Some Timer");
    4. someTimer = spawned.AddComponent<Countdown>();
    5. }
    This will create a new GameObject named "Some Timer" with a Countdown component attached, and then you can use "someTimer" elsewhere in your code to reference that one.
     
  5. qvbreugel

    qvbreugel

    Joined:
    Nov 13, 2017
    Posts:
    3
    Hey, thank for your reply.

    I just noticed I forgot to mention something in my post. I do not know the amount of countdowns I'd like to create. I haven't had any time to work on it yet, but I think the prefab option will be the best choice for this situation. Let me know, if you'd suggest otherwise.