What is wrong with this code?
pragma strict
var other : GameObject;
function Update () { function OnMouseDown() { other.GetComponent.().Play(); } }
Answer by TBruce · May 01, 2016 at 06:34 PM
@Dog Gamer
Actually there are several problems with your script
You can not place a function within a function
other.GetComponent.() is incorrect it should be other.GetComponent()
You need to pass a component type in to GetComponent like so other.GetComponent(SpriteRenderer)
Most components do not have a Play() method so unless your component type is something like an animation our AudioSource you may want to use something else like enable = true;
Below is an example
#pragma strict
var other : GameObject;
function OnMouseDown()
{
if (other != null)
{
other.GetComponent(SpriteRenderer).enabled = true;
}
}
@Dog Gamer Would you be so kind as to click the tick to accept the answer if your question was answered. Thank you!
Answer by Quertie · May 01, 2016 at 06:14 PM
You wrote:
other.GetComponent.().Play();
It actually is:
other.GetComponent().Play(); //Without the dot after "Component"
I hope this solves your problem :)
Your answer
Follow this Question
Related Questions
How to make a surface glow when its hit by a laser. 0 Answers
C# for applications vs C# for Unity help 3 Answers
how to use class or struct in unity api? 1 Answer
Spawn a dot for every number typed? 0 Answers