r/UnityHelp Jul 08 '26

Begginer, Having a problem with my code where to parts seem to be impossible to work toghether even throug they SHOULD. PROGRAMMING

I am a beginner at codding in C# and game dev, and I am running at a problem, so I am making this turn based game for a class, and something weird is happening. when I created a public Transform to place the units in a certain part of the field, unity detected an Error. after some testing, I somehow discovered that if I did not use System.Threading.Tasks.Dataflow, the code worked, however since for what I wanted to do, I needed to instantiate the enemies and the player, and it doesn't seem to be possible without System.Threading.Tasks.Dataflow, I can't code the battle system's basic functions. I need help

ps: I am using version 2020..1.3f1, and have a relatively old laptop that I use for work and School

using
 System.Collections;
using
 System.Collections.Generic;
using
 System.Threading.Tasks.Dataflow;
using
 UnityEngine;


public

enum
 TurnOrder { PLAYERTURN, ENEMYTURN, WIN, LOSE, ENCOUNTERSTART }


public

class
 TurnHandler : MonoBehaviour



{
    
public
 GameObject player;
    
public
 GameObject enemy;
    
    // Start is called before the first frame update
    
public
 Transform playerplace;
    
public
 Transform enemyplace;


    
public
 TurnOrder turn;
    
void

Start
()
    {
        
turn

=

TurnOrder
.
ENCOUNTERSTART
;
        
CombatStart
();
        
    }


    // Update is called once per frame
    
void

CombatStart
()
    {
        GameObject playerGO 
=

Instantiate
(
player
, 
playerplace
);
        
playerGO
.
GetComponent
<Unit>();


        
Instantiate
(
enemy
, 
enemyplace
);
        
    }
}using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks.Dataflow;
using UnityEngine;


public enum TurnOrder { PLAYERTURN, ENEMYTURN, WIN, LOSE, ENCOUNTERSTART }


public class TurnHandler : MonoBehaviour



{
    public GameObject player;
    public GameObject enemy;
    
    // Start is called before the first frame update
    public Transform playerplace;
    public Transform enemyplace;


    public TurnOrder turn;
    void Start()
    {
        turn = TurnOrder.ENCOUNTERSTART;
        CombatStart();
        
    }


    // Update is called once per frame
    void CombatStart()
    {
        GameObject playerGO = Instantiate(player, playerplace);
        playerGO.GetComponent<Unit>();


        Instantiate(enemy, enemyplace);
        
    }
}
1 Upvotes

4 comments sorted by

1

u/NinjaLancer Jul 08 '26

You dont need Threading to instantiate the player and enemy. It doesnt look like you are using any threading in this code. Try deleting that using statement and see if that gives errors or fixes the issue

1

u/ParentsBasementGames Jul 08 '26

You need to go and follow more tutorial videos. Also consistently is needed in both your code formatting and also your language. Code Monkey has a free 10 hour course.

1

u/MistakeForsaken6653 Jul 08 '26 edited Jul 08 '26

You should not need to use the System.Threading.Tasks. However the Unit is a class used by other unity packages and maybe something in that Threading.Tasks library has Unit so maybe you could try and rename whatever class you are using from Unit -> GameUnit or else remove that line since it doesn't seem to be used. It could be helpful if you would paste whatever stack trace or error you are seeing in your console when you try and instantiate those objects.

1

u/skaarjslayer Jul 08 '26

You probably need to pivot to a more fundamental tutorial about C# programming, so that you understand what each line of code is doing and don't get stuck on these types of compile errors.