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 timdadum · Jan 06, 2013 at 07:41 PM · variablecombattargettingburgzergarcade

Variable target in 'Targetting system' error.

Hi. I've been following the tutorials of BergZergArcade (Look him up on Youtube, he's awesome', but i came across this error:

UnassignedReferenceException: The variable target of 'PlayerAttack' has not been assigned. You probably need to assign the target variable of the PlayerAttack script in the inspector. PlayerAttack.Attack () (at Assets/Scripts/PlayerAttack.cs:33) PlayerAttack.Update () (at Assets/Scripts/PlayerAttack.cs:25)

My script:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Targetting : MonoBehaviour { public List targets; public Transform selectedTarget;

 private Transform myTransform;
 
 // Use this for initialization
 void Start () {
     targets = new List();
     selectedTarget = null;
     myTransform = transform;
     
     AddAllEneies();
 }
 
 public void AddAllEneies()
 {
     GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
 
     foreach(GameObject enemy in go)
         AddTarget(enemy.transform);
 }
 
 public void AddTarget(Transform enemy)
 {
     targets.Add(enemy);
 }
 
 private void SortTargetByDistance()
 {
     targets.Sort (delegate(Transform t1, Transform t2) { 
             return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
             });
 }
 
     
 private void TargetEnemy()
 {
     if(selectedTarget == null)
     {
         SortTargetByDistance();
         selectedTarget = targets[0];
     }
     else    
     {
         int index = targets.IndexOf(selectedTarget);
         
         if(index < targets.Count - 1)
         {
             index++;
         }
         else
         {
             index = 0;    
         }
         DeselectTarget();
         selectedTarget = targets[index];
     }
     SelectTarget();
 }
     
 private void SelectTarget()
 {
     selectedTarget.renderer.material.color = Color.red;
     
     PlayerAttack pa = (PlayerAttack)GetComponent("Playerattack");
     
     pa.target = selectedTarget.gameObject;
 }
 
 private void DeselectTarget()
 {
     selectedTarget.renderer.material.color = Color.green;
     selectedTarget = null;
 }
 
 // Update is called once per frame
 void Update () {
     if(Input.GetKeyDown(KeyCode.Tab))
     {
         TargetEnemy();    
     }
 }

}

So basically, i have a script called 'PlayerAttack' attached to my player. But i have 3 evil cubes, so how do i select more than one to be attached to the PlayerAttack script so i can attack all three. And last but not least, what if i get for example over 100 enemies, do i have to attach them all to the script? I hope some of you can help me out. Thanks alot!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by prototype7 · Jan 07, 2013 at 08:08 AM

Try to fix your error first

 public class Targetting : MonoBehaviour {
     public List<Transform> targets;
     public Transform selectedTarget;
     
     private Transform myTransform;
     void Start () {
         targets = new List<Transform>();
         selectedTarget = null;
         myTransform = transform;
         AddAllEnemies();
     }
 }
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

9 People are following this question.

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

Related Questions

Choose a Random Tag 1 Answer

Cannot lock onto enemies after they have been killed 1 Answer

Having trouble with slowly decreasing a variable 2 Answers

accessing vars of other script doesn't work 1 Answer

Need to use 2 different language scripts. 1 Answer


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