How to write a function in c#

using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemyscript : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform obstacles;
    float smallspawntime;
    private float speed = -10f;
    private Transform[] game;
    private GameObject[] obsspawnner;

    void Start()
    {   
        
    }

    // Update is called once per frame
    void Update()
    {   
        if (Time.time > smallspawntime)
        {   
            
            Vector3 Obstaclepos = new Vector3(Random.Range(-344f, 37f), -9.86f, 60);

            smallspawntime = Time.time + Random.Range(1f, 10f);
        }
        Vector3 obstaclemovement = new Vector3(0, 0, speed * Time.deltaTime);

        foreach (Transform obstacle in game) 
        {
            obstacle.transform.position += obstaclemovement;
        }

    }
    void Obstaclespawn(Transform[] game, Vector3 Obstacleposition)
    {
        game = new Transform[obstacles.childCount];
        Public Indexspawnner(Transform[] game, int Index)
        {
            game[Index] = transform.GetChild(Index);
            return game[Index];
        }
        int obsIndex = Random.Range(0, game.Length);
        var Indexspawning = Indexspawnner(game, obsIndex);
        GameObject obst = Instantiate(, Obstacleposition, Quaternion.Euler(0, 180, 0));
        obsspawnner.Add(obst.transform);
    }
}

I have some error in Obstaclespawn because I want to spawn child objects in a random time, how can I do that?

Then fix your errors. You don’t need us for that.

You can fix your own typing mistakes. Here’s how:

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Tutorials are a great place to start for these things. Nobody is going to type a fresh one for you in this tiny little text box, since with Unity it is ALWAYS more than just code.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

If you have an actual technical question, here is how to communicate it clearly:

How to report your problem productively in the Unity3D forums:

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.