I’m going through the Walker Boys’ Unity tutorials, and currently at an “On Your Own” section where you create a blinking sphere. Also, this is in Javascript BTW.
To do that, I use InvokeRepeating(“SphereFlash”,1.0,1.0); in the Start function, and have a function called SphereFlash. The line of code seems to run, since I have a print command right after that that gets printed to the console. The function I’m trying to invoke also seems to have been identified - if I change its name to Flash, I get the message Trying to Invoke method: scriptEnemy.SphereFlash couldn’t be called (the message does not appear when the function is labeled SphereFlash). However, the function itself never seems to run - I have print and Debug.Log commands right at the start of it, and neither get printed.
If it’s relevant, there are two objects in the scene running this particular script. As part of the tutorial, a different script is able to invoke a CountDown function without any hitches (disabling that didn’t change anything).
Any help would be appreciated. Thanks!
It is obviously running the function, but your function isn’t doing what you want it to do. So your real problem is: How to make an object flash? Post what you have right now so we can help.
Here’s the full code for enemyScript:
#pragma strict
// Enemy Script
// Inspector Variables
var shapeColor : Color[];
var numClicks : int = 2;
var respawnWait : float = 2.0;
var explosion : Transform;
var enemyPoint : int = 1;
var isSphere : boolean = false;
var sphereBlinkTime : float = 3.0;
var sphereCountdown : float;
// Private Variables
var storeClicks : int = 0;
var moving : boolean = false;
var targetVec : Vector3;
private var sphereRad : int = 0;
private var spherePhase : float = 0.0;
//var test : boolean = false;
function Start () { // called once before Update gets 1st call
storeClicks = numClicks;
var startPosVec = Vector3(Random.Range(-6,6),Random.Range(-4.2,4.2),0);
transform.position = startPosVec; // sets initially offscreen objects to random onscreen pos.
RandomColor();
sphereCountdown = sphereBlinkTime;
if (isSphere) {
InvokeRepeating("SphereFlash",1.0,1.0); //gets called, gets identified, no effect
print("Invoked");
}
}
function Update () {
var posVec;
if(numClicks <= 0) {
if (explosion) { // true if explosion references something
Instantiate(explosion, transform.position, transform.rotation); // create an explosion
}
if (audio.clip) {
audio.Play();
}
posVec = Vector3(Random.Range(-6,6),Random.Range(-4.2,4.2),0);
RespawnWait();
transform.position = posVec;
numClicks = storeClicks;
if (moving) {
targetVec = Vector3(Random.Range(-6,6),Random.Range(-4.2,4.2),0);
}
if (isSphere) {
//print("Cancelled");
CancelInvoke("SphereFlash");
InvokeRepeating("SphereFlash",1.0,1.0);
}
}
if (isSphere sphereCountdown <= 0.0) {
//print("Cancelled");
CancelInvoke("SphereFlash");
posVec = Vector3(Random.Range(-6,6),Random.Range(-4.2,4.2),0);
RespawnWait();
transform.position = posVec;
numClicks = storeClicks;
}
}
function SphereFlash () {
//test = true;
Debug.Log("Flashing");
renderer.enabled = false;
yield WaitForSeconds(0.5); // waits for 0.3 seconds
renderer.enabled = true;
}
// Time that G.O. is hidden after a click
function RespawnWait() {
renderer.enabled = false;
RandomColor();
yield WaitForSeconds(respawnWait); // waits for respawnWait seconds
renderer.enabled = true;
}
// Material of G.O. changed to random color
function RandomColor() {
if (shapeColor.length > 0) {
var newColor = Random.Range(0, shapeColor.length);
renderer.material.color = shapeColor[newColor];
}
}
The most relevant portions are the Start function at the beginning and the SphereFlash function nearer to the bottom. The two spheres have the isSphere property set to true - and I get “Invoked” printed twice when I play the scene (once for each of the spheres in the scene, but do not get “Flashing” showing up in the console, and do not observe any blinking.
Not sure how it works in javascript, but the problem might be due to using InvokeRepeating when SphereFlash is a coroutine.
Thanks for the help! I went and replaced the yield with a call to a new Wait function and put the yield in there. Seems to be working now. Strange behavior though, definitely wouldn’t have figured it out myself. Thanks again for the help!