Roatating a object that is randomly instantiated from a list:

I have a prefab/object that is instantiated from a list. Some time after it is instantiated I need to rotate the object(not knowing what object it is because it is basically hidden). Do I store the object so I can call for it to rotate?

I tried a few different things with no avail…:expressionless:

Does this even make sense???

Yes, if you need to do something later with your instantiated objects, you’ll need to store them in some sort of collection.

I’ve never done this before and I can not find any examples on how to do it. How would I go about doing this?

using System.Collections.Generic;

// Objects to instantiate that will show up in the inspector
public List<GameObject> objectsToInstantiate;

// The array of objects that we store our instantiated objects in
private List<GameObject> _storedObjects;

void Start()
{
    // Initialize our storage array
    _storedObjects = new List<GameObject>();
    
    // Iterate over our instantiate array and add each new item to our storage array
    for(int i = 0; i < objectsToInstantiate.Count; i++)
        _storedObjects.Add(Instantiate(objectsToInstantiate[i]) as GameObject);
    
    // Show in the console how many objects our storage array now holds
    Debug.Log("StoredObjects List has " + _storedObjects.Count + " objects stored in it.");
}

This shows the list “objectsToInstantiate” being instantiated and added to another list.

So I am instantiating the object from a list and then putting it in another list??

In my example there, yes. Although I’m not 100% sure if this is the solution you’re looking for.

How do I access the object after it is added tot he list?

This is how I am getting it from the list and instantiating it-

/
/
/
 GameObject dealer2card()    
    {        
        if(cards.Count == 0)        
        {         
            return null;   
            //shuffleDeck();       
        }         
        int card = Random.Range(0, cards.Count - 1);        
        GameObject go = GameObject.Instantiate(cards[card]) as GameObject;        
        cards.RemoveAt(card);       
        if(cards.Count == 0) 
        {
            shuffleDeck();     
        }
        return go;
    }
/
/
/
 void dealTOdealer()
    {
        GameObject oneCardD = dealer1card();
        GameObject twoCardD = dealer2card();
        if (oneCardD == null)
        {
            Debug.Log("Out of Cards");
            return;
        }
        if (twoCardD == null)
        {
            Debug.Log("Out of Cards");
            return;
        }
        oneCardD.transform.position = new Vector3(xPosD1, yPosD1, zPosD1);
        twoCardD.transform.position = new Vector3(xPosD2, yPosD2, zPosD2);
        twoCardD.transform.rotation = Quaternion.Euler(0, 180, 180);      
        hand.Add(oneCardD);
        hand.Add(twoCardD);      
        cardsDealt ++;
        cardsDealt ++; 
        }
/
/
/

After it is instantited i want to be able to change this line-

/
/
/
twoCardD.transform.rotation = Quaternion.Euler(0, 180, 180);  
/
/
/

to-

/
/
/
twoCardD.transform.rotation = Quaternion.Euler(0, 180, 0);  
/
/
/

It looks to me like you’re already storing the instantiated cards in your “hand” array.

public GameObject GetCardFromHand(GameObject searchObject)
{
    for (int i = 0; i < hand.Count; i++)
    {
        if (hand[i] == searchObject)
        {
            return hand[i];
        }
    }
    
    Debug.LogError("Can't find card " + gameObject.name);
    return null;
}

You could use something like this to search your hand array for a specific object, then modify the return object’s rotation like you have done in your previous post.

Example use:

GetCardFromHand(exampleObject).transform.rotation = Quaternion.Euler(0, 180, 0);

But there is four being instantiated at a time. that list is where they are stored before they are instantiated. I need to get a specific one that was instantiated and after some time rotate it like the above.

Could I use a raycast to get the object then rotate it??

You’re not getting helpful responses because your requirements are vague. All you say is “I need to rotate this thing later”. The obvious answer is to store a reference to it so you can access it later but for whatever reason that doesn’t seem to be appropriate in your case. The next obvious questions would be - why do you need to rotate it? What causes the rotate? Can you encapsulate that rotation event via some other means?

It’s also pretty clear that you tend to just copy-paste code right out of the forum and into your game. I would highly recommend that you not do that; one because you won’t learn anything by doing that and two it’s annoying to folks trying to help you because it seems like you’re not putting any effort into finding a solution yourself. (Your last post proves it. You asked - “Can I use a raycast to do this?” I dunno - can you? Did you try? What was the result?)

Lastly - it would be helpful if you provided more complete code snippets and more detailed error messages when you run into issues.

I am using an example for the base but have molded it to what I want to do. I am learning, I’ve never used lists or arrays and this is why I am doing it to learn.

I am not getting any errors. Obviously what I have tried on my own hasn’t worked because it was wrong. That’s why I am here.

The reason I need it to rotate is because it is part of the end game. I want it to rotate upon touching a GUITexture button.

I haven’t tried the raycast yet…I just thought of it today…I am at work and can’t do anything till this evening.

As I am sure you know Kelso…I usually figure things out on my own in time anyway…as I am learning.

There it is! The real requirement! :slight_smile:

Create a script that is attached to the GameObject with the GUITexture that stores a reference to the thing it needs to rotate. If necessary, turn that GUITexture + rotate script GameObject into a prefab.

Rgr that. Will work on it this evening.

Kelso I swear you follow me!

Sssshhhhhh :slight_smile:

Get a room guys…:expressionless:

Kelso, Kyle,

I achieved what I wanted to do but will you tell me if this is a good way of doing this or is it not good practice.-

/
/
/
private GameObject Flipcard;

//in the code to instantiate the object
Flipcard = twoCardD;

//in the button touch function I have this
Flipcard.transform.rotation = Quaternion.Euler(0, 180, 0);
/
/
/

It works perfectly.