• 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
Question by ThaiThao · Dec 12, 2011 at 03:10 AM · c#gamerpg

SelectTarget Does not exist Current Context

Hi there first of all i want to say i have been following BergZerg arcade tutorial on rpg game. But on tutorial 08-10 i ran into a problem as stated as the title. Here is the image on what the problem is Error

and here is the code

 using UnityEngine;

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

public class targeting : MonoBehaviour { public List targets; private Transform selectedTarget;

 private Transform myTransform;

 // Use this for initialization
 void Start () {
     targets = new List<Transform>();
     selectedTarget = null;
     myTransform = transform;
     
     AddAllEnemies();
 
 }
 
 public void AddAllEnemies()
 {
         GameObject[] go = GameObject.FindGameObjectsWithTag("enemy");
     
         foreach(GameObject enemy in go)
                 AddTarget(enemy.transform);
 }
 private void SortTargetsByDistance()
 {
         targets.Sort(delegate(Transform t1, Transform t2)    
     { return    Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));    });

 }
 public void AddTarget(Transform enemy)
 {
     targets.Add(enemy);
 }
 
 private void TargetEnemy()
 {
     if(selectedTarget == null)
     {
             SortTargetsByDistance();
             selectedTarget = targets[0];
     }
     else
     {
             int index = targets.IndexOf(selectedTarget);
         
             if(index < targets.Count - 1)
             {
                     index++;
             }
             else
             {
                     index = 0;
             }
             DeselectTarget();
             selectedTarget = targets[index];
             
     }
     SelectedTarget();
 }
 
 private void SelectTarget()
 {
         SelectedTarget.renderer.Marterial.color = Color.red;
     
     playerattack pa = (playerattack)GetComponent("playerattack");
     
     pa.target = selectedTarget.gameObject;
 }
 
 private void DeselectTarget()
 {
         selectedTarget.renderer.Marterial.color = Color.blue;
         selectedTarget = null;
 }
 
 // Update is called once per frame
 void Update () {
         if(Input.GetKeyDown(KeyCode.Tab))
         {
             TargetEnemy();
         }
 }

}

Comment

People who like this

0 Show 1
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 syclamoth · Dec 12, 2011 at 03:11 AM 0
Share

What is 'SelectedTarget'? is it a method, or an object, or a delegate, or what? I don't see where you declare it, and you can't seem to be able to decide what kind of thing it is.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Manny Calavera · Dec 12, 2011 at 04:53 AM

There are a couple of typos on your script:

  • You declare a private variable called "selectedTarget" but in the SelectTarget() method you refer to it as SelectedTarget -> Note the capital S.

  • The other problem is that you declared "SelectTarget()" method but then you call "Select*ed*Target()" -> Note the ED in the name

Comment

People who like this

0 Show 2 · 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 ThaiThao · Dec 12, 2011 at 05:10 AM 0
Share

thanks for that big pointer but how do i solve the material problem? and yes i already found out it was a typo.. but then it still say the same thing >_<

avatar image ThaiThao · Dec 12, 2011 at 05:17 AM 0
Share

nvm i solved it.. i just had to lower case it

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

WHY SAYS ME THAT IS AN ERROR? 1 Answer

Distribute terrain in zones 3 Answers

what am I doing wrong 1 Answer

Getting XP Script Help 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