Variable changing from other script does not work.
Good time of day to everybody. Currently, I am making a little strategy game in which you can select and command units. I have written a little script for selecting units on the gamefield. However, as is with any script that I write on my own, it does not work. It should work but it has two problems: 1. It does not recognize the isSeleted bool 2. It throws in this error "Expression denotes a method group, where a variable, value or type was expected." Here is my code:
void Update () {
units = GameObject.FindGameObjectsWithTag ("Unit");
if (Input.GetMouseButtonDown (0)) {
RaycastHit2D hit = Physics2D.Raycast (transform.position, Input.mousePosition);
if (hit != null && hit.collider != null) {
hit.transform.GetComponent<ShipAI>.isSelected = true;
}
}
}
Here is the script I am trying to access:
void Update (){
if (isSelected) {
Debug.Log ("I am the chosen one!");
}
}
Answer by doublemax · Oct 07, 2016 at 12:10 AM
hit.transform.GetComponent<ShipAI>.isSelected = true;
Should be:
hit.transform.GetComponent<ShipAI>().isSelected = true;
In order for this to work, the class "ShipAI" must also have a public variable "isSelected".
Thank you for your response. I have applied your solution, the error went away, however I still do not receiving the "I am the chosen one!" Debug.Log message.
Your answer
Follow this Question
Related Questions
Changing a variable in another script on Raycast hit - C# 1 Answer
Error upon rebooting Unity - "Associated script cannot be loaded" How can I resolve this? 2 Answers
Use of unassigned local variable 'startPos' " & " Use of unassigned local variable 'endPos' 1 Answer
UI panel without Image component as Raycast target? It is possible? 5 Answers
Keyboard Input Not Working For Certain Keys At Certain Points 0 Answers