- Home /
C# Script Not Working?
I have a C# script but it seems to not be doing what I want it to do?
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
//wall Boundaries
float wall_left;
float wall_right;
float wall_top;
float wall_bottom;
//AI properties
Vector2 AI_Position;
float Speed;
float randomX;
float randomY;
Vector2 randomXY;
Vector2 Direction;
private void Awake()
{
//Stuff you do on Awake.
Oh();
}
private void Oh ()
{
float Speed = 0.3f;
float wall_left = 5.0f;
float wall_right = Screen.width - 5.0f;
float wall_top = Screen.height - 5.0f;
float wall_bottom = 5.0f;
//Get two Random values within a Range (Screen dimensions)
float randomX = Random.Range(0,Screen.width);
float randomY = Random.Range(0,Screen.height);
//Create a Vector2 out of the Random values
Vector2 randomXY = new Vector2(randomX,randomY);
//Get the Direction from AI to the RandomXY generated
Vector2 Direction = randomXY - AI_Position;
//Normalize the Direction to apply appropriatly
Direction = Direction.normalized;
//Check that your AI is within your boundaries
if( AI_Position.x > wall_left && AI_Position.x < wall_right
&& AI_Position.y > wall_bottom && AI_Position.y < wall_top )
{
//Make AI move in the Direction (adjust speed to your needs)
AI_Position += Direction * Speed;
}
else
{
//make your AI do something when its not within the boundaries
//maybe generate a new direction?
}
}
}
Do you know what is wrong with it so I can fix it?
What do you want it to do? What is it doing or not doing?
Please tell us the error message, or we cant really help that much.
First step: read and follow some scripting/coding tutorials for C#, especially the parts about variable scope.
You are redefining most of your instance variables in Oh(). This means they are now local variable within the scope of that function, so any values you set won't be retained outside of at function. If you just want to set the values like 'Speed' just reference them by name without the type, so for example 'float Speed = 0.3f;' should be 'Speed = 0.3f;'. Same for Direction and randomXY etc.
Also as $$anonymous$$arkFinn says, if some thing isn't doing what you want it to do... You need to tell us what you want it to do!
Answer by clunk47 · Nov 24, 2013 at 07:32 PM
First, you don't need to define your type twice, only do that in the first part of your script. What I mean is you don't have to type 'float' in front of your variables inside of your function (void) because you already did this, so the variables are not local to the function. Also, you're only calling Oh() ONCE, in Awake(). Not sure why, you could just put all of your code inside of Awake() instead of using a separate method. But if you're trying to check for something all the time (every frame), you need to use Update(). Your question is vague, so I'll just throw your if() statements into Update() to show you what I mean. Try this, see how it goes, then get back at us with a better description of your issue.
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
//wall Boundaries
float wall_left;
float wall_right;
float wall_top;
float wall_bottom;
//AI properties
Vector2 AI_Position;
float Speed;
float randomX;
float randomY;
Vector2 randomXY;
Vector2 Direction;
private void Awake()
{
//Stuff you do on Awake.
Oh();
}
private void Oh ()
{
Speed = 0.3f;
wall_left = 5.0f;
wall_right = Screen.width - 5.0f;
wall_top = Screen.height - 5.0f;
wall_bottom = 5.0f;
//Get two Random values within a Range (Screen dimensions)
randomX = Random.Range(0,Screen.width);
randomY = Random.Range(0,Screen.height);
//Create a Vector2 out of the Random values
randomXY = new Vector2(randomX,randomY);
//Get the Direction from AI to the RandomXY generated
Direction = randomXY - AI_Position;
//Normalize the Direction to apply appropriatly
Direction = Direction.normalized;
}
void Update()
{
//Check that your AI is within your boundaries
if( AI_Position.x > wall_left && AI_Position.x < wall_right
&& AI_Position.y > wall_bottom && AI_Position.y < wall_top )
{
//Make AI move in the Direction (adjust speed to your needs)
AI_Position += Direction * Speed;
}
else
{
print ("Out of bounds");
//make your AI do something when its not within the boundaries
//maybe generate a new direction?
}
}
}
Answer by kensct · Nov 24, 2013 at 01:47 PM
You have defined "Speed" twice, in "Oh()" change "float Speed = 0.3f;" to "Speed = 0.3f;" or use "AI_Position += Direction * this.Speed;"
at the minute Speed will equal 0;
Your answer
Follow this Question
Related Questions
How do I animate my enemy randomly with time? 1 Answer
Distribute terrain in zones 3 Answers
How make script many people 0 Answers
Multiple Cars not working 1 Answer
looking to make for a zombie script AI,navmesh or not? 2 Answers