assign class objects instead of the actual script class to unity game objects in unity c#









up vote
1
down vote

favorite












I want to create a game in Unity C# in which the player will add as much as drones he needs as game objects to the game world. Therefore, I made a drone prefab and later in runtime I ask the number of drones the player wants to have (e.x. n numbers) and instantiate the prefab n times.
The script which is attached to the drone prefab is called drone_script.
Therefore, I am having a drone_script class as a general class. This class has an attribute (let's call it subscriber) which should have different unique values for each drone. So, it is created as a null reference in drone_script general class and later in runtime, I will initialize it.
During runtime, I create n (the same numbers as the drone game objects) objects from this class and assign their subscriber attribute different values.



This is how it looks:



 some_class

for (int i = 0; i < number_of_Drones; ++i)

drones_script[i] = new drone_script();
\ here I am creating n objects of my general drone_script class
drones_script[i].drone_subscriber = a unique value;
\ here I am assigning to each of the drone objects' drone_subscriber attribute a different value.
drco = new Vector3((float)0, (float)1, (float)0);
drones[i] = Instantiate(Resources.Load("drone"), drco,
Quaternion.Euler(-90, 0, 90)) as GameObject;
\I instatiate the drone prefab in unity game world here.




drone_script

public ROSBridgeSubscriber drone_subscriber;
void Start() \some code
void FixedUpdate () \some code



My problem is when the drone game objects are created in unity, the general class drone_script is attached to them and because of this their subscriber attribute is null. However, I need the drone_script[i] object to be assigned to them as their script, not the general drone_script itself. Because only then each drone game object has its corresponding subscriber attribute value from before even the start function is being called.
Am I thinking correctly?
If yes, how can I do it?










share|improve this question



























    up vote
    1
    down vote

    favorite












    I want to create a game in Unity C# in which the player will add as much as drones he needs as game objects to the game world. Therefore, I made a drone prefab and later in runtime I ask the number of drones the player wants to have (e.x. n numbers) and instantiate the prefab n times.
    The script which is attached to the drone prefab is called drone_script.
    Therefore, I am having a drone_script class as a general class. This class has an attribute (let's call it subscriber) which should have different unique values for each drone. So, it is created as a null reference in drone_script general class and later in runtime, I will initialize it.
    During runtime, I create n (the same numbers as the drone game objects) objects from this class and assign their subscriber attribute different values.



    This is how it looks:



     some_class

    for (int i = 0; i < number_of_Drones; ++i)

    drones_script[i] = new drone_script();
    \ here I am creating n objects of my general drone_script class
    drones_script[i].drone_subscriber = a unique value;
    \ here I am assigning to each of the drone objects' drone_subscriber attribute a different value.
    drco = new Vector3((float)0, (float)1, (float)0);
    drones[i] = Instantiate(Resources.Load("drone"), drco,
    Quaternion.Euler(-90, 0, 90)) as GameObject;
    \I instatiate the drone prefab in unity game world here.




    drone_script

    public ROSBridgeSubscriber drone_subscriber;
    void Start() \some code
    void FixedUpdate () \some code



    My problem is when the drone game objects are created in unity, the general class drone_script is attached to them and because of this their subscriber attribute is null. However, I need the drone_script[i] object to be assigned to them as their script, not the general drone_script itself. Because only then each drone game object has its corresponding subscriber attribute value from before even the start function is being called.
    Am I thinking correctly?
    If yes, how can I do it?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I want to create a game in Unity C# in which the player will add as much as drones he needs as game objects to the game world. Therefore, I made a drone prefab and later in runtime I ask the number of drones the player wants to have (e.x. n numbers) and instantiate the prefab n times.
      The script which is attached to the drone prefab is called drone_script.
      Therefore, I am having a drone_script class as a general class. This class has an attribute (let's call it subscriber) which should have different unique values for each drone. So, it is created as a null reference in drone_script general class and later in runtime, I will initialize it.
      During runtime, I create n (the same numbers as the drone game objects) objects from this class and assign their subscriber attribute different values.



      This is how it looks:



       some_class

      for (int i = 0; i < number_of_Drones; ++i)

      drones_script[i] = new drone_script();
      \ here I am creating n objects of my general drone_script class
      drones_script[i].drone_subscriber = a unique value;
      \ here I am assigning to each of the drone objects' drone_subscriber attribute a different value.
      drco = new Vector3((float)0, (float)1, (float)0);
      drones[i] = Instantiate(Resources.Load("drone"), drco,
      Quaternion.Euler(-90, 0, 90)) as GameObject;
      \I instatiate the drone prefab in unity game world here.




      drone_script

      public ROSBridgeSubscriber drone_subscriber;
      void Start() \some code
      void FixedUpdate () \some code



      My problem is when the drone game objects are created in unity, the general class drone_script is attached to them and because of this their subscriber attribute is null. However, I need the drone_script[i] object to be assigned to them as their script, not the general drone_script itself. Because only then each drone game object has its corresponding subscriber attribute value from before even the start function is being called.
      Am I thinking correctly?
      If yes, how can I do it?










      share|improve this question















      I want to create a game in Unity C# in which the player will add as much as drones he needs as game objects to the game world. Therefore, I made a drone prefab and later in runtime I ask the number of drones the player wants to have (e.x. n numbers) and instantiate the prefab n times.
      The script which is attached to the drone prefab is called drone_script.
      Therefore, I am having a drone_script class as a general class. This class has an attribute (let's call it subscriber) which should have different unique values for each drone. So, it is created as a null reference in drone_script general class and later in runtime, I will initialize it.
      During runtime, I create n (the same numbers as the drone game objects) objects from this class and assign their subscriber attribute different values.



      This is how it looks:



       some_class

      for (int i = 0; i < number_of_Drones; ++i)

      drones_script[i] = new drone_script();
      \ here I am creating n objects of my general drone_script class
      drones_script[i].drone_subscriber = a unique value;
      \ here I am assigning to each of the drone objects' drone_subscriber attribute a different value.
      drco = new Vector3((float)0, (float)1, (float)0);
      drones[i] = Instantiate(Resources.Load("drone"), drco,
      Quaternion.Euler(-90, 0, 90)) as GameObject;
      \I instatiate the drone prefab in unity game world here.




      drone_script

      public ROSBridgeSubscriber drone_subscriber;
      void Start() \some code
      void FixedUpdate () \some code



      My problem is when the drone game objects are created in unity, the general class drone_script is attached to them and because of this their subscriber attribute is null. However, I need the drone_script[i] object to be assigned to them as their script, not the general drone_script itself. Because only then each drone game object has its corresponding subscriber attribute value from before even the start function is being called.
      Am I thinking correctly?
      If yes, how can I do it?







      c# unity3d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 21:59









      Draco18s

      10k31940




      10k31940










      asked Nov 10 at 19:18









      zahra

      174




      174






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.



          Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:



          1. Instantiate drone and save his reference (as GameObject)

          2. Get that drone's "drone_script" (using GetComponent<>(); )

          3. Save that script reference in our array so we can use it later

          4. Adjust some variables of that script now or later in the game

          Something like that should work. In code everything should be something like this (didn't test, might be some miss types)



          some_class

          public GameObject dronePrefab;
          for (int i = 0; i < number_of_Drones; ++i)

          // Step 1.
          drco = new Vector3((float)0, (float)1, (float)0);
          drones[i] = Instantiate(dronePrefab, drco,
          Quaternion.Euler(-90, 0, 90)) as GameObject;
          // Steps 2 & 3
          drones_script[i] = drones[i].GetComponent<drone_script>();
          // Step 4
          drones_script[i].drone_subscriber = a unique value;




          EDIT:
          Just to point it out: drone_script has to be attached to drone prefab






          share|improve this answer






















          • Correction: drones[i].GetComponent<drone_script>();, the T parameter needs to be the class, not a string of the class's name.
            – Draco18s
            Nov 10 at 20:20










          • Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
            – zahra
            Nov 10 at 20:34










          • The solution is not working. I tried it.
            – zahra
            Nov 10 at 21:40










          • I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
            – rCgLT
            Nov 11 at 21:09










          • You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
            – CaTs
            Nov 14 at 1:24










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242558%2fassign-class-objects-instead-of-the-actual-script-class-to-unity-game-objects-in%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.



          Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:



          1. Instantiate drone and save his reference (as GameObject)

          2. Get that drone's "drone_script" (using GetComponent<>(); )

          3. Save that script reference in our array so we can use it later

          4. Adjust some variables of that script now or later in the game

          Something like that should work. In code everything should be something like this (didn't test, might be some miss types)



          some_class

          public GameObject dronePrefab;
          for (int i = 0; i < number_of_Drones; ++i)

          // Step 1.
          drco = new Vector3((float)0, (float)1, (float)0);
          drones[i] = Instantiate(dronePrefab, drco,
          Quaternion.Euler(-90, 0, 90)) as GameObject;
          // Steps 2 & 3
          drones_script[i] = drones[i].GetComponent<drone_script>();
          // Step 4
          drones_script[i].drone_subscriber = a unique value;




          EDIT:
          Just to point it out: drone_script has to be attached to drone prefab






          share|improve this answer






















          • Correction: drones[i].GetComponent<drone_script>();, the T parameter needs to be the class, not a string of the class's name.
            – Draco18s
            Nov 10 at 20:20










          • Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
            – zahra
            Nov 10 at 20:34










          • The solution is not working. I tried it.
            – zahra
            Nov 10 at 21:40










          • I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
            – rCgLT
            Nov 11 at 21:09










          • You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
            – CaTs
            Nov 14 at 1:24














          up vote
          1
          down vote



          accepted










          First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.



          Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:



          1. Instantiate drone and save his reference (as GameObject)

          2. Get that drone's "drone_script" (using GetComponent<>(); )

          3. Save that script reference in our array so we can use it later

          4. Adjust some variables of that script now or later in the game

          Something like that should work. In code everything should be something like this (didn't test, might be some miss types)



          some_class

          public GameObject dronePrefab;
          for (int i = 0; i < number_of_Drones; ++i)

          // Step 1.
          drco = new Vector3((float)0, (float)1, (float)0);
          drones[i] = Instantiate(dronePrefab, drco,
          Quaternion.Euler(-90, 0, 90)) as GameObject;
          // Steps 2 & 3
          drones_script[i] = drones[i].GetComponent<drone_script>();
          // Step 4
          drones_script[i].drone_subscriber = a unique value;




          EDIT:
          Just to point it out: drone_script has to be attached to drone prefab






          share|improve this answer






















          • Correction: drones[i].GetComponent<drone_script>();, the T parameter needs to be the class, not a string of the class's name.
            – Draco18s
            Nov 10 at 20:20










          • Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
            – zahra
            Nov 10 at 20:34










          • The solution is not working. I tried it.
            – zahra
            Nov 10 at 21:40










          • I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
            – rCgLT
            Nov 11 at 21:09










          • You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
            – CaTs
            Nov 14 at 1:24












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.



          Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:



          1. Instantiate drone and save his reference (as GameObject)

          2. Get that drone's "drone_script" (using GetComponent<>(); )

          3. Save that script reference in our array so we can use it later

          4. Adjust some variables of that script now or later in the game

          Something like that should work. In code everything should be something like this (didn't test, might be some miss types)



          some_class

          public GameObject dronePrefab;
          for (int i = 0; i < number_of_Drones; ++i)

          // Step 1.
          drco = new Vector3((float)0, (float)1, (float)0);
          drones[i] = Instantiate(dronePrefab, drco,
          Quaternion.Euler(-90, 0, 90)) as GameObject;
          // Steps 2 & 3
          drones_script[i] = drones[i].GetComponent<drone_script>();
          // Step 4
          drones_script[i].drone_subscriber = a unique value;




          EDIT:
          Just to point it out: drone_script has to be attached to drone prefab






          share|improve this answer














          First of all, I suggest you to assign dronePrefab using editor or in Start method of "some_class" because Resources.Load("drone") might be resource consuming, since you load a drone resource "n" times.



          Now your specific problem, you don't need to create new instances of "drone_script" because as you mentioned yourself, each drone that you create has his own "drone_script". So basically what we need to do is:



          1. Instantiate drone and save his reference (as GameObject)

          2. Get that drone's "drone_script" (using GetComponent<>(); )

          3. Save that script reference in our array so we can use it later

          4. Adjust some variables of that script now or later in the game

          Something like that should work. In code everything should be something like this (didn't test, might be some miss types)



          some_class

          public GameObject dronePrefab;
          for (int i = 0; i < number_of_Drones; ++i)

          // Step 1.
          drco = new Vector3((float)0, (float)1, (float)0);
          drones[i] = Instantiate(dronePrefab, drco,
          Quaternion.Euler(-90, 0, 90)) as GameObject;
          // Steps 2 & 3
          drones_script[i] = drones[i].GetComponent<drone_script>();
          // Step 4
          drones_script[i].drone_subscriber = a unique value;




          EDIT:
          Just to point it out: drone_script has to be attached to drone prefab







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 at 0:44

























          answered Nov 10 at 20:10









          rCgLT

          662




          662











          • Correction: drones[i].GetComponent<drone_script>();, the T parameter needs to be the class, not a string of the class's name.
            – Draco18s
            Nov 10 at 20:20










          • Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
            – zahra
            Nov 10 at 20:34










          • The solution is not working. I tried it.
            – zahra
            Nov 10 at 21:40










          • I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
            – rCgLT
            Nov 11 at 21:09










          • You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
            – CaTs
            Nov 14 at 1:24
















          • Correction: drones[i].GetComponent<drone_script>();, the T parameter needs to be the class, not a string of the class's name.
            – Draco18s
            Nov 10 at 20:20










          • Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
            – zahra
            Nov 10 at 20:34










          • The solution is not working. I tried it.
            – zahra
            Nov 10 at 21:40










          • I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
            – rCgLT
            Nov 11 at 21:09










          • You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
            – CaTs
            Nov 14 at 1:24















          Correction: drones[i].GetComponent<drone_script>();, the T parameter needs to be the class, not a string of the class's name.
          – Draco18s
          Nov 10 at 20:20




          Correction: drones[i].GetComponent<drone_script>();, the T parameter needs to be the class, not a string of the class's name.
          – Draco18s
          Nov 10 at 20:20












          Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
          – zahra
          Nov 10 at 20:34




          Thank you so much for your answer. Do I need to attach drone_script to drone prefab too?
          – zahra
          Nov 10 at 20:34












          The solution is not working. I tried it.
          – zahra
          Nov 10 at 21:40




          The solution is not working. I tried it.
          – zahra
          Nov 10 at 21:40












          I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
          – rCgLT
          Nov 11 at 21:09




          I've edited as @Draco18s pointed out. Yes, drone_script must be a part of the drone prefab. What is the problem that it is not working ? Debug and write us what is the problem, but I believe it should be ok if everything is done correctly
          – rCgLT
          Nov 11 at 21:09












          You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
          – CaTs
          Nov 14 at 1:24




          You can change the type of dronePrefab to the drone_script if it is a MonoBehaviour and save yourself from having to do GetComponent.
          – CaTs
          Nov 14 at 1:24

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242558%2fassign-class-objects-instead-of-the-actual-script-class-to-unity-game-objects-in%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党