Forum rules - please read before posting.

Activating\deactivating GameObjects

edited November 2014 in Engine development
Hello,

not sure if this is already doable but I wanted to suggest an addition into the Action type: object.
Basically,

Right now you can Add\Remove objects, that's true, but for example I have an object that's already invisible (it just emits light) as it's a child of the camera and that I need to activate only later in the game. This is probably doable via script but it would be nice if I could Enable it through an interaction. 
Basically, like the "send message turn on\off" action which of course doesn't work on gameobjects though.


Ignore this post if there's already another way to do it :p

just my 2 cents
«13

Comments

  • Perhaps, I'll think about it.
  • //Part 1 activate game object action
    using UnityEngine;
    using System.Collections;


    #if UNITY_EDITOR
    using UnityEditor;

    namespace AC
    {
    [System.Serializable]
    public class ActionMyActivate : Action
    {
    public GameObject myActivate;
    public ActionMyActivate ()
    {
    this.isDisplayed = true;
    title = "Object: Activate Object";
    }
    override public float Run ()
    {
    myActivate.SetActive (true);
    if (!isRunning)
    {
    isRunning = true;
    return defaultPauseTime;
    }
    else
    {
    isRunning = false;
    return 0f;
    }
    }
  • //part 2 activate game object


    #if UNITY_EDITOR

    override public void ShowGUI ()
    {
    myActivate = (GameObject) EditorGUILayout.ObjectField ("Object to activate:", myActivate, typeof (GameObject), true);
    }
    override public string SetLabel ()
    {
    string labelAdd = "";
    if (myActivate)
    labelAdd = " (" + myActivate.name + ")";
    return labelAdd;
    }

    }
    }
  • The above should be pasted into an action template.cs
    As far as I am aware it works but I haven't tested it fully. It does load into the actions list, however, maybe @Chris would be kind enough to look it over as there may be some quirks in AC I'm not familiar with.
    A deactivate action can be done simply by making a few changes.
  • That's very very kind of you. I'll test it tomorrow.
  • @David, I forgot to put the 
    AfterRunningOption ();   in. It gets inserted in part 2  on the line below the piece that starts 'myActivate =  '

    It puts in the continue or stop after running option.
  • edited November 2014
    Hey,
    sorry for the late update but, so far, so good! I tested it in a opening cutscene on a disabled game object, and it seemed to work.

    Is it troublesome if I ask for a deactivate one, too ? I'd guess it would be about reversing the code in if (isRunning) but I don't want to make a disaster and then have to call for help again.

    So thank you so much @sicga123
    ChrisIceBox  if you could confirm that the code is "clean", I'd appreciate it! :)

    edit of an edit: nvm sorry, had a minor problem but solved. It didn't had to do with the Action.
  • @David - sorry I don't come here often. Basically just replace the word myActivate to 
    myDeactivate. Then change where myDeactivate.SetActive(true)   to myDeactivate.SetActive(false);

    That should do it.
  • woops, that was fairly obvious actually, shame on me.  Thanks
  • edited November 2014
    @sicga123 ;
    uh-oh, spoke too soon. Unfortunately, it seems that this breaks completely when I go on to Build - my fault for not testing this before. 
    Basically, whatever gameobject was attached to the action is replaced back with None when the build is created. This affects both the build itself and the editor (in the sense that if you don't set it back, there will still be "none"). So, right now, it's impossible to use if you build =(.

    Either that, or my Unity is getting crazy.
    Damn, I'm dead without this
  • Hi David,

    I haven't tested it to any great degree, I'll put up a new project and see if I can build a demo scene with a couple of things to test and see what happens. I need to use this as well so I'll try and figure it out.
    I'll do it as soon as I can. 


  • Hi David,

    I just tested both thse actions in the AC demo scene - the basement - first in play mode then in build, I had no problem. it built and ran and didn't crash.

    I assume you've done the following steps but I will state them here just in case:

    1. put the actions in the AC scripts/actions folder
    2. refresh the actions list in the game editor actions tab

    make sure the actions are pointed at your game manager assets.

    If this doesn' solve the error you will need to provide more details.

  • edited November 2014
    Thanks, damn, I was hoping you were able to reproduce it... this means that it's going to be another nightmare to track down the source of the problem, sigh. 

    Naturally, yes, I had done all you said, infact they work, just, mysteriously, the game object gets "lost" when the compiling happens. First, I will test this again in a new project like you said. I will get back to you asap. Sorry for the pain :/

    edit: okay, just tested in a new scene, same project. Via cutscene this time, a simple cube, it remains there. Apologies, should've done this test before going panic mode. Hmm. Doing more tests
    2) copied the key part of the action list (it's a long one :( ) into a cutscene (it was an asset file) to start on scene load, works fine, object remains. Gap gets smaller, at least.. next...

    3) okay, ran other 10+ different test. This is the current situation that creates the problem, will ran a few more:

    - the action is inside a newly created asset, and not run through a cutscene.
    - it's run on the standard interaction ->  "use" of the inventory.
    - doesn't matter what game object I try to activate, still gets 'disconnected' when I Build

    have not tried to recreate this specific situation in a new scene yet...
    edit: tried again, new scene, same project. Bug happens again. Next I'm going to test in a new project...
  • Hi David,

    What version of Unity are you using? Is it windows or mac?

    When you state 'newly created asset' do you mean or are instantiating an object at runtime or do you mean the object is in the scene but just switched off and it gets loaded along with everything else?
  • edited November 2014
    Hi, 4.5.5f Pro, I can test on a free version if needed but it's very unlikely this is making any difference. Win7 64bit.

    Apologies, the "created asset" was bad wording on my part: I just meant that it's an action list file (actionlist.asset). Obvious, since it's the only way to run this from the Standard Interactions in the inventory.
    The object that needs to be activated is already present in the scene, it is simply ticked off.
    I'm gonna test the same conditions on another project as soon as possible... thank you
  • Okay, to be honest I haven't delved into AC that much. If you could post a screenshot of the inventory manager and the actionlist that uses these actions possibly I can spot something. Is it possible to post images here? I don't know I'm an infrequent visitor myself and haven't quite worked out how this forum works.

    I am using the pro version as well so no need to test on the free. 


  • edited November 2014
    Just tested in a new project, I confirm that it happens with those conditions. 
    If you still have problems, I can easily pack it up as smallish as I can for you.

    Before trying this, would you mind to upload me your  two .cs files for Activate\Deactivate so that I can check if they're identical to mine? I doubt they're different or it wouldn't even work in Play, but I'd like to give it a last check.

    edit: actually I don't know what happened, but after I cleaned the new project and tested it again, while the effect in Unity is the same (I noticed that the gameobject was back to "none" in the AL after I made the build), in the build itself, it "kind of freezed" - cursor disappeared and movement was blocked, but ESC menu was working, lol. Kind of weird, before it just made a console error appear with the usual "object reference not set to an instance of an object bla bla"
  • Hi David
    I added another section to my original post that included putting in the usual option to continue into the .cs, basically this :
    AfterRunningOption ();   It gets inserted in part 2  on the line below the piece that starts 'myActivate =  '
    Did you put that into the scripts?

    As regards uploading them, that doesn't seem possible with this forum, at least I can't see a button that allows that. I have no problem forwarding them to your email if you want to tell me what it is.




  • edited November 2014
    Hi, 
    yes I had already added that.
    You can just make a .zip file and upload it on http://zippyshare.com/ or similar websites. Then you just need to copy paste the resulting link. Since in this case all the code is public you can either post it here, or just click on my nickname, then click on the "Message" button to send me a private message. I will send you my test project if the scripts are the same so that you can replicate the error (hopefully)
    and thanks
  • edited November 2014
    Hi David
    Okay, I tested it by putting the actionscripts in an assetlist and running it then. I replicated the problem by doing this. I partially solved it by changing the myActivate.SetActive(true);

    to

    GameObject newObject = myActivate;
    newObject.SetActive (true);

    So it works in the scene, it builds but doesn't run in the game. This could be because I am a total noob with AC and it may be an assetlist needs to be put in a resources folder to run, unfortunately I have no time to look into it further tonight.

    Maybe it will build and run for you. Let me know the result and I'll investigate further if you have no luck with it.


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.