Forum rules - please read before posting.

Custom Action help needed: SetActive (GameObject) / false, true

edited August 2014 in Technical Q&A
I'm working on a custom action script that I will be needing a lot throughout the game play which will be used on TriggerEnter to call an action:
Find Game Object by name, or by what object dragged in the Inspector, then to either set it Active(true) or false.

So far I've struggled and got this to work by drag and dropping the Object into the action but I have not been able to change the option to allow me to find an object by name :)

/*
 *
 *    Adventure Creator
 *    by Chris Burton, 2013-2014
 *    
 *    "ActionSendMessage.cs"
 * 
 *    This action calls "SendMessage" on a GameObject.
 *    Both standard messages, and custom ones with paremeters, can be sent.
 * 
 */

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using AC;

#if UNITY_EDITOR
using UnityEditor;
#endif

[System.Serializable]
public class ActionSetActive : Action
{
    
    public int constantID = 0;
    public int parameterID = -1;
    public GameObject linkedObject;
    public string WhichName = null; // to be used for whichever object to be found by name.
    
    public ActionSetActive ()
    {
        this.isDisplayed = true;
        title = "Object: Set Active";
    }
    
    
    override public void AssignValues (List<ActionParameter> parameters)
    {
        linkedObject = AssignFile (parameters, parameterID, constantID, linkedObject);
    }
    
    
    override public float Run ()
    {
        if (linkedObject)
        {
                linkedObject.SetActive(true);
        }
        
        return 0f;
    }
    
    
    override public int End (List<AC.Action> actions)
    {
        // If the linkedObject is an immediately-starting ActionList, don't end the cutscene
        if (linkedObject && linkedObject.GetComponent <Cutscene>())
        {
            Cutscene tempAction = linkedObject.GetComponent<Cutscene>();
            
            if (tempAction.triggerTime == 0f)
            {
                return -2;
            }
        }
        
        return (base.End(actions));
    }
    
    
    #if UNITY_EDITOR
    
    public override void ShowGUI (List<ActionParameter> parameters)
    {
        parameterID = ChooseParameterGUI ("Object to affect:", parameters, parameterID, ParameterType.GameObject);
        if (parameterID >= 0)
        {
            constantID = 0;
            linkedObject = null;
        }
        else
        {
            linkedObject = (GameObject) EditorGUILayout.ObjectField ("Object to affect:", linkedObject, typeof(GameObject), true);
            
            constantID = FieldToID (linkedObject, constantID);
            linkedObject = IDToField  (linkedObject, constantID, false);
        }

        AfterRunningOption ();
    }
    
    #endif
    
}
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.