Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by androgcyborgsam · May 01, 2016 at 04:37 PM · c#beginnerscript error

Unity C# question

I'm having some issues trying to work on a script for a FPS. I keep getting errors that something is wrong in my script and I'm not sure how to fix it. Mind you, I'm fairly new with C#.

Some of the errors I am getting are these: alt text

If anyone can help me out with this, it would be greatly appreciated. Thank you.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterSystem))]
 
 public class AICharacterController : MonoBehaviour
 {
 
     public GameObject ObjectTarget;
     public string TargetTag = "Player";
     private CharacterSystem character;
     private int aiTime = 0;
     private float scanFrequency = 1.0f;
     private int aiState = 0;
 
     void Start()
     {
         character = gameObject.GetComponent<CharacterSystem>();
         InvokeRepeating("ScanForTarget", 0, scanFrequency);
     }
 
     void ScanForTarget()
     {
         ObjectTarget = GetNearestTaggedObject();
     }
 
     public GameObject GetNearestTaggedObject()
     {
         var nearestDistanceSqr = Mathf.Infinity;
         GameObject nearestObj = null;
 
         foreach (var obj in GameObject.FindGameObjectsWithTag(TargetTag))
         {
 
             var objectPos = obj.transform.position;
             var distanceSqr = (objectPos - transform.position).sqrMagnitude;
 
             if (distanceSqr < nearestDistanceSqr)
             {
                 nearestObj = obj;
                 nearestDistanceSqr = distanceSqr;
             }
         }
         return nearestObj;
     }
 
     public float DistanceSquaredTo(GameObject source, GameObject target)
     {
         return Vector3.SqrMagnitude(source.transform.position - target.transform.position);
     }
 
     void Update()
     {
         //if (GameObject.Find("CharacterDakota").GetComponent<CharacterStatus>().HP > 0) {
 
         if (GameObject.Find("GameManager").GetComponent<GameManager>().Playing)
         {
 
             var direction = Vector3.zero;
             if (aiTime <= 0)
             {
                 aiState = Random.Range(0, 4);
                 aiTime = Random.Range(10, 100);
             }
             else {
                 aiTime--;
             }
             if (ObjectTarget)
             {
                 //ObjectTarget = GameObject.FindGameObjectWithTag(TargetTag);   
                 float distance = Vector3.Distance(ObjectTarget.transform.position, this.gameObject.transform.position);
 
                 if (distance <= 2)
                 {
                     transform.LookAt(ObjectTarget.transform.position);
                     if (aiTime <= 0)
                     {
                         if (aiState == 1)
                         {
                             character.Attack();
                         }
                     }
                 }
                 else {
                     if (aiState == 1)
                     {
                         transform.LookAt(ObjectTarget.transform.position);
                         direction = this.transform.forward;
                         direction.Normalize();
                         character.Move(direction);
                     }
                 }
 
             }
             else {
                 ScanForTarget();
             }
         }
     }
 }


cerrors.png (159.0 kB)
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 Quertie · May 01, 2016 at 04:42 PM -1
Share

First of all, does this work in Unity? If this is the case, then the problem comes from the fact you are using Visual Studio.

avatar image androgcyborgsam Quertie · May 01, 2016 at 04:54 PM 0
Share

Not sure how I would know it works in unity. When I go into unity, that's where I open up my C# to do scripting. I saved the project file for C# and then when I try to play the game, it tells me that I need to correct the errors before I can enter playmode.

avatar image Quertie androgcyborgsam · May 01, 2016 at 05:31 PM -1
Share

Oh ok... And are all of the scripts (charactersystem, gamesystem and the current script) in the same folder?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tuncOfGrayLake · May 02, 2016 at 05:16 AM

You may be missing the CharacterSystem in your project folder or the Visual Studio's solution.

First one you can fix by importing the package. Second one you can fix through Visual Studio by closing and reopening the solution.

Not sure if something else is wrong here.

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
0

Answer by needforbleed · May 02, 2016 at 05:14 AM

Solution 1

I didn't use RequireComponent before but, at least I guess so, that function tries to get the Component of type XY and uses it in your script. From what I have found in the scripting reference of RequireComponent, I would say that you simply didn't add a Component of type CharacterSystem to your Gameobject which the script is attached to.


Solution 2

You didn't add Character System correctly to your Project. From what I have seen in your script you never added a reference for CharacterSystem in your script. To do so, just add " namespace CharacterSystem{ //Script.......} " at the start of your scripts.


Solution 3

CharacterSystem is broken. Typo somewhere.


Solution 4

I guess you probably want to get the CharacterSystem Component attached to your gameobject. I wouldn't use RequireComponent for that. If that's what you wanted to achieve, just define a variable of type CharacterSystem and assign the value at runtime.

 public CharacterSystem char;
 
 void start()
 {
 char=getComponent<CharacterSystem>();
 
 }

This is what you did as well. There's probably no need to Require any Component since you're already trying to get the reference with getComponent.

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[RESOLVED! :D][C#] Friction/Deceleration - Compiler won't let me damp individual Rigidbody axes to 0. Force in opposite direction maybe? 2 Answers

C# for beginners in Unity for multi-platforms? 1 Answer

How do I update the position of different prefabs to the current position? 0 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