I am not really a C# person but with my limited knowledge, I am fairly sure the code below is correct, but I can’t get it to work because the code closes itself early.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveScript: MonoBehaviour
{
public void Start()
{
public GameObject self = GameObject.FindWithTag("Fast");
public int speed = 255;
public int decreaseArea = self.transform.localScale.x * self.transform.localScale.y;
public int distance = 0;
}
public void Update()
{
speed -= decreaseArea;
distance += speed;
self.transform.Translate(Vector3.forward * speed);
}
}
The MonoBehavour class tries to close itself on the } at the end of Start(). I have rearranged, cut and pasted back in, even changed editors; once changing to a plain text editor and Unity still didn’t like it.
I would be thankful for anything that could get me out of this muddling and paradoxical problem.
I don’t know what “tries to close itself” means. As a rule of thumb, if you get any sort of error message, copy and paste the exact message. Details are super important in programming.
But I’m pretty sure your problem is caused by the fact that you are trying to declare “public” variables inside of Start(). public/private/protected modifiers can only put on class members, not local variables.
You really need to learn the syntax of C#…
Just for once, try this, but be aware, I wrote it without trying it, since I don’t have the means, it may contain problems.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveScript: MonoBehaviour
{
public GameObject _self;
public int speed = 255;
public int decreaseArea;
public int distance;
public void Start()
{
_self = GameObject.FindWithTag("Fast");
decreaseArea = _self.transform.localScale.x * _self.transform.localScale.y;
}
public void Update()
{
speed -= decreaseArea;
distance += speed;
_self.transform.Translate(Vector3.forward * speed);
}
}
After converting a few ints and floats, it worked
Final Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveScript : MonoBehaviour
{
public GameObject _self;
public int speed = 255;
public float decreaseArea;
public int distance;
public void Start()
{
_self = GameObject.FindWithTag("Fast");
decreaseArea = _self.transform.localScale.x * _self.transform.localScale.y;
}
public void Update()
{
speed -= ((int)decreaseArea);
distance += speed;
_self.transform.Translate(Vector3.forward * (speed));
}
}