Search Unity

many objects with same script but need to make changes locally

Discussion in 'Scripting' started by Krodil, Jun 25, 2013.

  1. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    hey,
    been messing (up) with this for a while.
    I have x amount of objects being spawned with the same script.
    In the script I have a variable that should check if the particular object is active.
    nevertheless every object becomes active.
    I tried both static var and public var but no luck so far.

    some objects will have the same name (e.g. sphere(clone)), some not.

    In the script below I wish to change the material for the object being clicked:

    Code (csharp):
    1. function Awake () {
    2.    
    3.     materialOriginal = this.gameObject.renderer.material;
    4. }
    5. var objectMode : boolean = false;
    6. public var materialOriginal : Material;
    7. public var materialWhenObjectActive : Material;
    8. function Update () {
    9.     if(objectMode == true  tag=="object")
    10.     {
    11.         materialWhenObjectActive = modeControl.activeObject.renderer.material;
    12.     }
    13.     else
    14.     {
    15.         this.gameObject.renderer.material = materialOriginal;
    16.     }
    17.    
    18.  
    19. }
    Right now I wish to use the color shift as visual feedback that the touch on the particular obj is performed..

    hope to get some pointers, dunno why this is going wrong.. too much coffee?
    Best,
    Krodil
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how are you changing the objectMode variable? there's nothing in the post code that changes it from false to true...
     
  3. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    I do that in a function that 'listens for Tap (touch input) <-- that works.
    code:

    Code (csharp):
    1. function OnTap( gesture : TapGesture )
    2. {
    3.     //var a = GameObject("_GUI").GetComponent(modeControl);
    4.     if(gesture.Selection.gameObject.tag == "notAnObject"){
    5.         Debug.Log( "ACTIVE " + gesture.Position + " on " + gesture.Selection.name );
    6.         objectMode = false;
    7.         modeControl.objectMode1 = false;
    8.        
    9.        
    10.  
    11.         modeControl.objectName = gesture.Selection.name;
    12.         print(objectMode);
    13.         modeControl.activeObject = null;
    14.        
    15.     }
     
  4. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    That piece of code changes the objectMode to false. But what changes it to true?
     
  5. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    Also, where do you assign materialWhenObjectActive to the renderer's material?
     
  6. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    I have an if statement looking for another Tag, just did not paste it.

    Basically, how can you have a lot of objects with same scripts, but only change one of these objects from another script?
    I cant do it by name since they are called the same. By tag is also unwanted since I might have 500 objects with the same script.
     
  7. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    Well, they don't have to be named the same. But if you don't like to use name then implement your own "object id" functionality. I've done it a # of times and it works well. It will not let you search for a specific object if you have 100's of them running around but it will let you distinguish between them if you got an instance of one of them through touch for example. You can even implement a generic "object pool" component. People do it for things like "bullet pool", etc...
     
  8. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    that sounds interesting, can you share some more of this?

    Im thinking that when e.g. sphere(clone) is touched it will be assigned as a placeholder (var activeObject: gameObject) on another script.
    You think that could be viable?