- Home /
How to Read & Write Integers to Text File
Each time I pass through one of the following objects "PathA", "PathB" etc I want the text file to add to an integer every time. So if you play 15 times and pass through paths A, B and C, 7, 7 or 1 times this will be shown in a text file, updating every playthrough.
I've made an attempt with the knowledge and research i've made as you can see below but each time i pass through PathA it doesn't continue to add and just keeps displaying as 1. I would guess its something fairly simple but something i'm not aware of or doing properly.
Ultimately what I'm looking for is depending on which path the player passes through most, this will alter the difficulty of the game. In my case, spawning more enemies in those locations. Each play through having which path was passed through stored in a text file as an integer
Hoping whoever reads understands what i'm looking for lol :)
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Text;
public class Pathing : MonoBehaviour
{
int PathA;
public void OnTriggerEnter (Collider withPlayer)
{
string path = @"E:\Year 3\Individual Project\FinalProject\Assets\Pathing.txt";
//!File.Exists(path) &&
// This text is added only once to the file.
if (gameObject.tag == "PathA" && withPlayer.gameObject.tag == "Player") {
// Create a file to write to.
//PathA++;
string createText = "Player went through Path A" + Environment.NewLine;
//string createText2 = "Number of times: " + PathA + Environment.NewLine;
File.AppendAllText (path, createText);
//File.AppendAllText(path, createText2);
}
if (gameObject.tag == "PathB" && withPlayer.gameObject.tag == "Player") {
// Create a file to write to.
string createText = "Player went through Path B" + Environment.NewLine;
File.AppendAllText (path, createText);
}
if (gameObject.tag == "PathC" && withPlayer.gameObject.tag == "Player") {
// Create a file to write to.
string createText = "Player went through Path C" + Environment.NewLine;
File.AppendAllText (path, createText);
}
// Open the file to read from, display the entire file
string readText = File.ReadAllText (path);
print (readText);
print (PathA);
Destroy (gameObject);
}
}