Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Send message Help

Discussion in 'Scripting' started by Jsmucker, Dec 15, 2014.

  1. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    I am having trouble with getting sendmessage to work I've tried using:
    GameObject.SendMessage: Returns error "An object reference is required to access non-static member"
    Component.SendMessage: Returns error "An object reference is required to access non-static member"
    EDIT - gameObject.SendMessage: Returns error "The name `gameObject' does not exist in the current context"
    SendMessage: Returns error "The name `SendMessage' does not exist in the current context"
    I have the code here: Line 14 is where I have experimented with changing the format.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using RAIN.Action;
    5. using RAIN.Core;
    6.  
    7. [RAINAction]
    8.  
    9. public class TakeDamage : RAINAction
    10. {
    11.  
    12.     public override void Start(AI ai)
    13.     {
    14.         GameObject.SendMessage("Damage", 1.0f, SendMessageOptions.DontRequireReceiver);
    15.         base.Start(ai);
    16.     }
    17.  
    18.     public override ActionResult Execute(AI ai)
    19.     {
    20.         return ActionResult.SUCCESS;
    21.     }
    22.  
    23.     public override void Stop(AI ai)
    24.     {
    25.         base.Stop(ai);
    26.     }
    27. }
    I am using RAIN so I can't just make another variable and put that at the beginning of sendmessage.
     
    Last edited: Dec 15, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    Thanks for the reply, but using gameObject.SendMessage returns the error: "The name `gameObject' does not exist in the current context"
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You can only send messages from actual game objects. That means you need a reference to a game object or your script has to inherit MonoBehaviour.
     
  5. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    Would I be able to have the code inherit a monobehavior without changing anything that the code is doing?
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It looks like you are already inheriting RAINAction (it doesn't seem to be an interface), which means you can't.
    I don't know who should receive the SendMessage, but you may try ai.gameObject.SendMessage. I think ai is a component, so this may work.