Skip to content

could someone who is familiar with C# please help me out with my game script?

Removed before it was archived.

2 points4 comments
Open on Reddit
About this post
Posted
Archived
Removed on
Score
2 · 100% up
Kind
Text
ID
cft9id

Author

3
posts
3
comments
2
media
View profile

4 comments

Here is your code opened in Visual Studio Community 2017 (which is a free tool)

Then I use Edit | Advanced | Format document

You've got a loop with no {}

You forgot to close your update routine. Put a } in.

Here is your fixed code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class tilemanager : MonoBehaviour
{
    public GameObject[] tilePrefabs;

    private Transform playerTransform;
    private float spawnZ = 0.0f;
    private float tileLegnth = 12.44f;
    private int amnTilesOnScreen = 7;
    // Start is called before the first frame update

    private void Start()
    {
        playerTransform = GameObject.FindGameObjectsWithTag("Player").transform;
        for (int i = 0; i < amnTilesOnScreen; i++)
        {
            // You forgot these {}
        }


    }

    // Update is called once per frame
    private void Update()
    {
        {
            if (playerTransform.position.z > (spawnZ - amnTilesOnScreen * tileLegnth))
            {
                SpawnTile();
            }
        }
    } // You missed this } here.

    private void SpawnTile(int prefabIndex = -1)
    {
        GameObject go;
        go = Instantiate(tilePrefabs[0]) as GameObject;
        go.transform.SetParent(transform);
        go.transform.position = Vector3.forward * spawnZ;
        spawnZ += tileLegnth;
    }

}
4 points

thanks for the reply, I'm just getting one error message now, think you could help me with fixing it?

ErrorCS1061 'GameObject[]' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?) Assembly-CSharp C:\Users\Jimmy\endless runner\Assets\tilemanager.cs 17 Active

1 points

playerTransform = GameObject.FindGameObjectsWithTag("Player").transform

This is calling transform on an array of game objects, which isn't something you can do.

Instead of using FindGameObjectsWithTag use FindGameObjectWithTag. This is assuming you only have one GameObject with that tag "Player."

Or if you need it to be an array, you need to choose an index in that array to get the transform from.

example: playerTransform[2];

2 points

From https://docs.unity3d.com/ScriptReference/GameObject.html

Note: Many variables in the GameObject class have been removed. To access, for example GameObject.renderer in csharp use GetComponent<Renderer>() instead.

You'll find this a lot with older tutorials. The method of obtaining components has changed. Use the following instead in your Start function;

playerTransform = GameObject.FindGameObjectsWithTag("Player").GetComponent<Transform>()
1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.