scripts controlling scripts

I’ve been playing with this all day and haven’t been able to make any progress. I have a “laser” that shoots automatically and is one of six random colors. I have a game object with a script that changes into the same 6 colors randomly. The idea is that this game object will act as “preview” of what next laser show will be (like bubble shoot).

As an example: lets say the preview is red. As soon as the preview changes to the next color (lets just say Blue) a red laser will shoot. And since the preview is Blue, we know the next laser will be blue. And so on, and so on, and so on…

The idea is that the changing preview will trigger the firing of the laser, this way I hope I can just change the timing of the preview to control how fast and slow the whole thing works.

// Preview
var myMaterials : Material[];
var delay : float; // seconds between color changes

 function Start () {

    InvokeRepeating ("ChangeColor", delay, delay);

}

 function ChangeColor () {

    renderer.material = myMaterials[Random.Range (0, myMaterials.Length)];

}
//Auto Fire Script

var lastFireTime : float;
var fireDelay : float = 3.0f;
var laserBeamPrefab : Transform;
function Update()

{
	if(Time.time > lastFireTime + fireDelay)

    {

        var laserBeam = Instantiate(laserBeamPrefab, gameObject.Find("spawnPoint").transform.position,Quaternion.identity);
    
    	laserBeam.rigidbody.AddForce(Vector3.forward * 100);

        
        lastFireTime = Time.time;
        


    }

 

}

where are you setting the instantiate’d laserBeam’s material?

I add the materials through the inspector via var myMaterials : Material[ ]; On the first script above. That first script works fine for changing the preview object. I’m yet to find a way to to have that color change be the event that instantiates the laserBeam shot :frowning:

Instead of using the “Update” method to handle the shooting, you could simply call “SendMessage” right before or right after you change the color too, couldn’t you? I guess I don’t quite understand what you are getting at but if you change “Update” to “ShootLaser”

    // Preview
    var myMaterials : Material[];
    var delay : float; // seconds between color changes
     
     function Start () {
     
        InvokeRepeating ("ChangeColor", delay, delay);
     
    }
     
     function ChangeColor () {
     
        renderer.material = myMaterials[Random.Range (0, myMaterials.Length)];
        SendMessage("ShootLaser")
    }

I’m pretty new to scripting, can you elaborate on the “SendMessage” technique?

Sorry for not being clear. You know how in Tetris, when you’re working with fitting one of the peaces, and on the side you can see what the next shape is going to be? I’m trying to do the same thing but with the colors of my “lasers.” My idea is that if my preview color can control the Instantiation of the laser fire, then I can control the whole rhythm of things with that one peace ( like a set driven key in Maya).

I use GetComponent to access any script, from some other script.

Lets say you want to tell your player to move. However, you press your button to do so. You have a player script, and a button script. By pressing the button, you will want to acess the player right?

So use GetComponent, it is like SendMessage(). Anyhow…

Code might look like this.

///BTN SCRIPT
var player : GameObject;
var playerData : playerScript;

function Start(){
///by getting the playerScript compinent of player, you can access that script
playerData = player.GetCoponent(playerScript);
}

function btnPressed(){
playerData.playerMove();
}

Now, since we have the playerScript component attached to player, we can get into that function. We can control, adjust, manipulate data from one script, btnScript, to the other, playerScript.

///PLAYER SCRIPT
function playerMove(){
///this function was called from the btnScript
transform.position = newPosition;
}

“SendMessage” executes the method on the given object. So say you have script A with method B and a script Y with method Z both attached to the same game object.

Inside method B you call SendMessage(“Z”) it will execute method Z from inside Y - this way you can avoid grabbing components if you don’t want to have to worry about that. SendMessage can be called on any object you can have access too, for example in a collision - collider.SendMessage(Z) would execute message Z from inside Y on the object that collided with it.

^^ I remember reading SendMessage had a higher overhead then calling a component that has already been retrieved. Is that true? As I think about it, I can’t see why, but I do recall seeing that.

:confused:

I would assume it is the case that it’s true, because the engine would have to go through and dig out the correct script that has the function.

However - there is also the trade off storing stuff in memory can cause problems for lower end machines - granted this would have to be in a massive scale of references between scripts, but it is definitely possible.

These trade-offs and over heads are also only extremely important in an environment where the game is a very serious game with memory or timing concerns - the standard learning game generally hits neither of these to soon.

So, yes, I would definitely, in most cases use GetComponent, as SendMessage does need to find each script on object, then go thru each script, to see if it has a function SendMessage asked for.

Hey renman3000, I’m using your “mover player” example to try and get this working, but I think I’m over my head on this one. Can you show me (or anyone else) where my errors are?

Again, this first script is for my “color preview,” and the goal is to have the changing of the color preview instantiate the laserBeam (with the same color beam as shown in the preview) in the second script.

// Preview
var myMaterials : Material[];
var delay : float; // seconds between color changes
var laserBeam : GameObject;
var playerData : LaserFire;

  function Start () {

    InvokeRepeating ("ChangeColor", delay, delay);
	
}

 function ChangeColor () {

    playerData = laserBeam.GetCoponent(LaserFire);
    renderer.material = myMaterials[Random.Range (0, myMaterials.Length)];
	playerData.PlayerMove();
	
}
//Auto Fire based on preview 

var lastFireTime : float;
var fireDelay : float = 3.0f;
var laserBeamPrefab : Transform;

function laserBeam()



    {
		
		
        var laserBeam = Instantiate(laserBeamPrefab, gameObject.Find("spawnPoint").transform.position,Quaternion.identity);
    
    	laserBeam.rigidbody.AddForce(Vector3.forward * 100);

        lastFireTime = Time.time;
        

    }

When I start the game, I get the following error message:

MissingMethodException: UnityEngine.GameObject.GetCoponent
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create ()
Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[ ] args)
Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[ ] args)
Boo.Lang.Runtime.RuntimeServices+c__AnonStorey14.<>m__7 ()
Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[ ] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[ ] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[ ] args, System.Type scriptBaseType)
laserPreview.ChangeColor () (at Assets/Scripts/laserPreview.js:15)

I was able to correct the error is issue message issue I was getting. But still can’t seem to get it to work :frowning: