Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CB-TV · Nov 24, 2013 at 12:04 PM · c#airandom

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkFinn · Nov 24, 2013 at 12:32 PM 1
Share

What do you want it to do? What is it doing or not doing?

avatar image littlepsycho · Nov 24, 2013 at 01:39 PM 0
Share

Please tell us the error message, or we cant really help that much.

avatar image Huacanacha · Nov 24, 2013 at 06:08 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?
         }
     }
     
     
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges