1

This has been driving me crazy and I have been at this with no luck for hours.

All I want to do is run one of my scripts from another script.

Both scripts are attached to the same game object. Here's the script I want to use to run the other script.

using UnityEngine;
using System.Collections;

public class RedTeam : MonoBehaviour {

public Wander wanderScript;

void Awake(){
    wanderScript = GetComponent<Wander>();
}

void Update(){ 
    wanderScript();
} 
}

Here is my wander script...

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]

public class Wander : MonoBehaviour
{
public float speed = 5;
public float changeDirectionTime = 1;
public float maxDegreesToChange = 30;

CharacterController controller;
float heading;
Vector3 targetRotation;

void Awake ()
{
    controller = GetComponent<CharacterController>();

    // Set random rotation
    heading = Random.Range(0, 360);
    transform.eulerAngles = new Vector3(0, heading, 0);

    StartCoroutine(NewHeading());
}

void Update ()
{
    transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * changeDirectionTime);
    var forward = transform.TransformDirection(Vector3.forward);
    controller.SimpleMove(forward * speed);
}

IEnumerator NewHeading ()
{
    while (true) {
        NewHeadingRoutine();
        yield return new WaitForSeconds(changeDirectionTime);
    }
}

void NewHeadingRoutine ()
{
    var floor = Mathf.Clamp(heading - maxDegreesToChange, 0, 360);
    var ceil  = Mathf.Clamp(heading + maxDegreesToChange, 0, 360);
    heading = Random.Range(floor, ceil);
    targetRotation = new Vector3(0, heading, 0);
}
}

This is the error I am getting.

error CS1955: The member `RedTeam.wanderScript' cannot be used as method or delegate

My main goal is to be able to enable and disable the Wander script from my TeamRed script.

user3071284
  • 6,955
  • 6
  • 43
  • 57
iEpic
  • 95
  • 2
  • 12
  • 1
    A class is an object, and methods are things it does. Just like you don't say 'I will go do house!', you cannot just invoke the concept of wandering. You may need to do some C# tutorials. – Magus Apr 01 '14 at 21:00
  • What do you mean you want to "call the whole thing"? Do you want to add a new instance/copy of the `Wander` script/component to the same `GameObject`? EDIT: The way Unity works, it will execute the `Update` method on the attached `Wander` script _automatically_; you do not (and arguably) should not be invoking it manually. – Chris Sinclair Apr 01 '14 at 21:00
  • When Wander is activated (and enabled) it will do the _whole thing_. Awake, Update, Start they are all called automatically – Kay Apr 01 '14 at 21:07
  • Basically I want to be able to enable or disable the Wander script from my RedTeam script. – iEpic Apr 01 '14 at 21:08
  • I guess I could just use do wanderScript.enabled Wow, I can't believe it was that simple. I spent so long trying to figure it out, and I was way off. – iEpic Apr 01 '14 at 21:17
  • you ***really*** should do some OOP/C# tutorials before trying to do a game. – Roberto Apr 01 '14 at 22:21

1 Answers1

2

If you want to enable/disable the Wander script from your RedTeamScript, do the following in your RedTeamScript...

wanderScript = GetComponent<Wander>();
wanderScript.enabled = false;

or

wanderScript = GetComponent<Wander>();
wanderScript.enabled = true;

Note : GetComponent will only work because the Wander script is on the same gameObject. If the wander script was on another gameObject you would need to get a reference to that gameObject first and call GetComponent on it.

It is also more efficient to declare

wanderScript = GetComponent<Wander>();

In your Start method so GetComponent is only called once.

Greg Quinn
  • 1,927
  • 1
  • 23
  • 26