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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How do I access a script attached to a GameObject in a way what won't be overly verbose

Discussion in 'Scripting' started by woodkir000, Apr 8, 2022.

  1. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    I'm trying to make 2 arrays of a GameObject and have made a script for the prefab, but I've found that I can't just make an instance of the script and instantiate the GameObject from there because MonoBehavior can't be made with the "new" keyword, but also there doesn't seem to be an easy way to use the script if I've got a lot of instances of a GameObject and want to be able to use an instance of the script for each instance of the GameObject
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    Steps to success:

    - have a reference to the GameObject

    - call .AddComponent<MyScriptName>() on it

    - keep a reference to what that method returns.

    I like this structure to maintain sanity:

    Factory Pattern in lieu of AddComponent (for timing and dependency correctness):

    https://gist.github.com/kurtdekker/5dbd1d30890c3905adddf4e7ba2b8580
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    My current thought it, perhaps the purpose of your script or how you are trying to use it is not correct. Are you certain you need this to be a MonoBehaviour script? Looking at your other post, I think the script was PlayerSprite or something like that, which I assume controlled the sprite of the player. But is it a class that simply holds an array of sprites the player could be? It may be helpful to show a bit of your code and what you are trying to accomplish and perhaps we can give you some additional advice.

    And of course, use code tags. :D
     
    Kurt-Dekker likes this.
  4. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    Here, I uploaded the code onto GitHub so you can see it: https://github.com/AndrewC243/Unity
    Please don't code review as I know it probably really sucks, I'm just trying to troubleshoot a project I'm doing with some friends for our Video Game Programming assignment. Line 22 and 26 is where I formerly made instances of PlayerSprite for each array, until I realized that the reason my game wasn't working was because I can't make instances of a class extending MonoBehaviour or something. I'll add comments to it to explain my thinking on various parts of the code when I post this comment
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    I mean, we have to review the code if we're going to help...

    Code (CSharp):
    1.  async void Start()
    2.    {
    3.        for (int x = 0; x < 10; x++) {
    4.            for (int z = 0; z < 10; z++) {
    5.                Instantiate(prefab, new Vector3(x, 0, z), Quaternion.identity); // Create ground tiles
    6.            }
    7.        }
    8.  
    9.        t1sprites = new PlayerSprite[4]; // Initialization, 4 sprites per tean
    10.        t2sprites = new PlayerSprite[4];
    11.  
    12.        for (int i = 0; i < t1sprites.Length; i++) {
    13.                t1sprites[i] = PlayerSprite.inst(i / 2, (float)0.9, 0);
    14.        }
    15.      
    16.       /*
    17.         Formerly:
    18.        
    19.         for (int i = 0; i < t1sprites.length; i++) {
    20.             t1sprites[i] = new PlayerSprite();
    21.             t1sprites[i].inst(i * 2, (float)0.9, 0);
    22.         }
    23.        
    24.       */
    25.  
    26.         for (int i = 0; i < t2sprites.Length; i++) {
    27.                t1sprites[i] = PlayerSprite.inst(i / 2, (float)0.9, 0);
    28.        }
    29.        
    30.    }
    There is an error here in your second for loop, I'll let you see if you can spot it.
    Looks like your PlayerSprite is your player class basically.
    I would suggest your Init class exist in the scene with the prefab for your PlayerSprite on it and have it instantiate them instead.

    I'm not sure how your Test variable is getting a value in the PlayerSprite class for you to be able to instantiate it.

    Since you made changes and aren't using new anymore, that message should be gone.
     
  6. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    Hm, I'm a bit confused, so should I move things from one class to the other? If I do make my arrays as GameObjects, how do I make the init script use the methods of PlayerSprite, or should I not and just see what I can change to the Init script?

    If it helps, I believe I forgot to include that my Init script is attached to a transparent plane