The question was answered and solved, unity is not for beginners and should not be used to learn programming. OP should learn the basics of C# first.
error CS0305: Using the generic type `System.Collections.Generic.List' requires `1' type argument(s)
i get the error
error CS0305: Using the generic type System.Collections.Generic.List<T>' requires
1' type argument(s)
here's my script
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Collections.Generic.List;
public class friendlyTurretAI : MonoBehaviour {
public enum AIstates { NEAREST, FURTHERST, WEAKEST, STRONGEST };
public AIstates aiState = AIstates.NEAREST;
FrindlyTrackingSystem m_tracker;
friendlyShoot m_shooter;
friendlyRotate m_range;
// Use this for initialization
void Start () {
m_tracker = GetComponent<FrindlyTrackingSystem>();
m_shooter = GetComponent<friendlyShoot>();
m_range = GetComponent<friendlyRotate>();
}
// Update is called once per frame
void Update () {
if (!m_tracker || !m_shooter || !m_range)
return;
switch (aiState)
{
case AIstates.NEAREST:
TargetNearest;
break;
case AIstates.FURTHERST:
TargetFurthest;
break;
case AIstates.STRONGEST:
break;
case AIstates.WEAKEST:
break;
}
}
void TargetNearest()
{
List<GameObject> validTargets = m_range.GetValidTargets();
GameObject curTarget = null;
float closestDist = 0.0f;
for(int i = 0; i < validTargets.count; i++)
{
float dist = Vector3.Distance(transform.position, validTargets[i].transform.position);
if(!curTarget || dist < closestDist)
{
curTarget = validTargets[i];
closestDist = dist;
}
m_tracker.SetTarget(curTarget);
m_shooter.SetTarget(curTarget);
}
}
void TargetFurthest()
{
List<GameObject> validTargets = m_range.GetValidTargets();
GameObject curTarget = null;
float furthestDist = 0.0f;
for (int i = 0; i < validTargets.count; i++)
{
float dist = Vector3.Distance(transform.position, validTargets[i].transform.position);
if (!curTarget || dist < furthestDist)
{
curTarget = validTargets[i];
furthestDist = dist;
}
m_tracker.SetTarget(curTarget);
m_shooter.SetTarget(curTarget);
}
}
}
what have I done wrong?
Answer by LoqicxDev · Jun 25, 2017 at 11:44 PM
I would suggest you show us your function GetValidTargets()
@LoqicxDev im guessing you mean the script that gets the valid targets here it is using System.Collections; using System.Collections.Generic; using UnityEngine; public class friendlyRotate : $$anonymous$$onoBehaviour { List m_targets = new List(); public List tags;
void OnTriggerEnter(Collider other) { bool invalid = true; for(int i = 0; i < tags.Count; i++) { if (other.CompareTag(tags[i])) { invalid = false; break; } } if (invalid) return;
m_targets.Add(other.gameObject);
}
void OnTriggerExit(Collider other) { for(int i = 0; i < tags.Count; i++) { if(other.gameObject == m_targets[i]) { m_targets.Remove(other.gameObject); return; } } }
public List GetValidTargets() { for (int i = 0; i < m_targets.Count; i++) { if (m_targets[i] == null) { m_targets.RemoveAt(i); i--; } }
return m_targets;
}
public bool InRange(GameObject Go) { for(int i = 0; i < m_targets.Count; i++) { if (Go == m_targets[i]) return true; }
return false;
} }
You cannot just use List
as variable, you must give it a type like int or string
public List tags; //this is already a problem
public List GetValidTargets() //this one too
I believe they should be List<GameObject>
Answer by TheRussianBrit · Jul 03, 2017 at 04:24 PM
i did this but I still get the error
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class friendlyRotate : MonoBehaviour {
List<GameObject> m_targets = new List<GameObject>();
List<GameObject>
OnTriggerEnter(Collider other)
{
bool invalid = true;
for(int i = 0; i < tags.Count; i++)
{
if (other.CompareTag(tags[i]))
{
invalid = false;
break;
}
}
if (invalid)
return;
m_targets.Add(other.gameObject);
}
void OnTriggerExit(Collider other)
{
for(int i = 0; i < tags.Count; i++)
{
if(other.gameObject == m_targets[i])
{
m_targets.Remove(other.gameObject);
return;
}
}
}
List<GameObject> GetValidTargets()
{
for (int i = 0; i < m_targets.Count; i++)
{
if (m_targets[i] == null)
{
m_targets.RemoveAt(i);
i--;
}
return m_targets;
}
return m_targets;
}
public bool InRange(GameObject Go)
{
for(int i = 0; i < m_targets.Count; i++)
{
if (Go == m_targets[i])
return true;
}
return false;
}
}
you serious?
List<GameObject> m_targets = new List<GameObject>();
List<GameObject>
@ShadyProductions what did I do wrong
the 2nd list is redundant and should not be there, its a compile error. the fact that you do not know this means you do not understand the basics of program$$anonymous$$g. And you should not be trying to learn program$$anonymous$$g in unity. Unity is not for beginners.
Follow this Question
Related Questions
How to create a script for a vehicle fuel? 0 Answers
ArgumentOutOfRangeException is occuring when it shouldn't 0 Answers
Manual GUI Refresh 0 Answers
Using Polymorphism Override 1 Answer
What is the best way to make moving obstacles in a 3d game? 0 Answers