- Home /
Unity giving me some c sharp errors, cs1061
Hello Unity community! I am getting a problem from a script that I am trying to switch over from unity 4 to 5 in the asset bundle "Simply A*". I am getting three errors, all of level cs1061. I have looked over a few other posts and none have worked in my context. Here's the script:`using UnityEngine; using System.Collections; using System.Collections.Generic;
public class TDManager : MonoBehaviour { public GameObject start; public GameObject end; public GameObject tower; public GameObject ghostTower; public GameObject enemy; public Renderer rend;
private List<GameObject> towers = new List<GameObject>();
void Start()
{
StartCoroutine(SpawnEnemy());
rend = GetComponent<Renderer>();
}
void Update ()
{
StartCoroutine(PlaceTowers());
}
private RaycastHit CheckPosition()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
//Make sure to set towers in a grid, by rounding position to an int
Vector3 newPos = hit.point;
newPos.Set(Mathf.RoundToInt(newPos.x) - 0.5F, 0.4F, Mathf.RoundToInt(newPos.z) + 0.5F);
ghostTower.transform.position = newPos;
//Set color of "show" tower based on the spot being available
if (hit.transform.tag == "Ground")
{
ghostTower.rend.material.color = Color.green;
}
else
{
ghostTower.rend.material.color = Color.red;
}
}
else
{
ghostTower.rend.material.color = Color.red;
}
//Return all hit information which we use later
return hit;
}
private IEnumerator PlaceTowers()
{
RaycastHit hit = CheckPosition();
bool canPlace = false;
//Make sure that we did hit something
if (hit.transform != null)
{
canPlace = (hit.transform.tag == "Ground") ? true : false;
}
if (Input.GetButtonDown("Fire1") && canPlace)
{
GameObject newTower = Instantiate(tower, new Vector3(Mathf.RoundToInt(hit.point.x) - 0.5F, 0.3F, Mathf.RoundToInt(hit.point.z) + 0.5F), Quaternion.identity) as GameObject;
towers.Add(newTower);
yield return new WaitForEndOfFrame();
Pathfinder.Instance.InsertInQueue(start.transform.position, end.transform.position, CheckRoute);
}
}
private void CheckRoute(List<Vector3> list)
{
//If we get a list that is empty there is no path, and we blocked the road
//Then remove the last added tower!
if (list.Count < 1 || list == null)
{
if (towers.Count > 0)
{
GameObject g = towers[towers.Count - 1];
towers.RemoveAt(towers.Count - 1);
Destroy(g);
}
}
}
IEnumerator SpawnEnemy()
{
yield return new WaitForSeconds(1.5F);
GameObject e = Instantiate(enemy, start.transform.position, Quaternion.identity) as GameObject;
e.GetComponent<TDEnemy>().start = start.transform.position;
e.GetComponent<TDEnemy>().end = end.transform.position;
StartCoroutine(SpawnEnemy());
}
} The errors are as follows:
Assets/Pathfinding/Scenes/Scripts/TDManager.cs(42,28): error CS1061: Type UnityEngine.GameObject' does not contain a definition for
rend' and no extension method rend' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)` then Assets/Pathfinding/Scenes/Scripts/TDManager.cs(46,28): error CS1061: Type
UnityEngine.GameObject' does not contain a definition for rend' and no extension method
rend' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
and finally, Assets/Pathfinding/Scenes/Scripts/TDManager.cs(51,24): error CS1061: Type
UnityEngine.GameObject' does not contain a definition for rend' and no extension method
rend' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Any help would be appreciated, thanks!
Answer by flaviusxvii · Apr 25, 2015 at 02:56 AM
canPlace = (hit.transform.tag == "Ground") ? true : false;
the == comparison is already returning a boolean value. It's confusing to wrap it with the ternary operator and just return the exact same value.
ghostTower.rend
The variable 'ghostTower' is of type GameObject. "Type UnityEngine.GameObject' does not contain a definition for rend'" means that class GameObject does not have anything called "rend" in it.
You used to be able to directly access the renderer of a GameObject. Not anymore.
You'll have to do:
ghostTower.GetComponent<Renderer>().material.color = Color.green;
Your answer
Follow this Question
Related Questions
Unity 5 API Updater Keeps Failing 4 Answers
"Error building Player" WebGL build error 2 Answers
Sound files not working [Unity 5.1] 0 Answers
mac unity fbx import error 0 Answers