Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Grinch91 · Mar 26, 2014 at 01:03 PM · c#raycasting

Selecting an Object

Ok so I am a little unsure with a few things about selecting an Object. So what I am trying to do is when you click on an object from one parent, provided the resources are there it will move the object, to a new parent. So for example in my game you click on Card A from Player's Hand, it checks resources and then either moves it to Player's battlefield or nothing if resources aren't there.

I am looking at Raycasting currently and my main question is how do I actually move the object, from one parent to the other all information remaining too? This is what I am trying but doesn't seem to be working.

 public class MouseController : MonoBehaviour {
 
     GameObject storeObj = new GameObject();
 
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
 
     public void OnMouseDown()
     {
         Debug.Log ("Mouse Down");
         if(hit.collider != null)
         {            
             storeObj = hit.collider.gameObject;
             transform.parent.gameObject.GetComponent<Game>().OnCardHandClick(storeObj);
         }
     }
 }

Also it is throwing an error from my function that creates the objects. Saying I cannot call this function when declaring a variable. Which I am confused about.

     void AddToPlayerHand()
     {
         CardDef c1 = playerDeck.Deal();
         if(c1 != null)
         {
             GameObject newObj = new GameObject();
             newObj.name = "Card";
             CardAttributes newCard = newObj.AddComponent<CardAttributes>();
             BoxCollider bc = newObj.AddComponent<BoxCollider>();
             bc.size = new Vector3(tempX,tempY,1);
             SpriteRenderer ren = newObj.AddComponent<SpriteRenderer>();
             string store = "battleChariot";//playerDeck.deck.CardDef.Back;
             Sprite mySprite = Resources.Load<Sprite>(store)as Sprite;
             newCard.Data = c1;
             newObj.transform.parent = playerHand.transform;
             float x = -6+(playerDeck.deck.Count)*2.0f;
             float y = -5.8f;
             ren.sprite = mySprite;
             Vector2 pos = new Vector2(x,y);
 //This line is where it is pointing me to with the error            
             newObj.AddComponent<MouseController>();
             newObj.transform.position = pos;
 
         }
 
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Grinch91 · Mar 27, 2014 at 05:16 PM

Ended up solving this.

 private Player playerTemp;
     private GameObject gameInstance;
 
     void OnMouseDown()
     {
         playerTemp = new Player();
         temp = gameObject.GetComponent<CardAttributes>().Data.Name;
         gameInstance = GameObject.Find("Game");
         Debug.Log ("Mouse Down");
         if(playerTemp.playerSupply >= gameObject.GetComponent<CardAttributes>().Data.Cost)
         {
             Debug.Log ("Yay");
             Debug.Log ("Temp contains");
             Debug.Log(temp);
             gameInstance.GetComponent<Game>().OnCardHandClick(temp);
             Destroy(this.gameObject);
         }
         else
         {
             Debug.Log ("Nay");
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by diegzumillo · Mar 26, 2014 at 01:55 PM

To change the parent of something simply change the variable transform.parent. You can assign any gameobject to that variable.

Raycasting is one way to go, if you don't have the card/gameobject referenced in your script. It depends on how your game is structured. If this is a card game, I would keep all the player's cards as a big array on his script and simply cycle through that array to perform actions.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Grinch91 · Mar 26, 2014 at 02:33 PM 0
Share

I'm really struggling with this, I just can't get what I want to work to actually work. Basically a card is generated as a newObj and placed into the playerHand. What I want to do then is to take the newObj GameObject once it is clicked and move it into playerActive which is the play area for the player.

Basically my $$anonymous$$ouseController script is like this now:

 public class $$anonymous$$ouseController : $$anonymous$$onoBehaviour {
 
     public void On$$anonymous$$ouseDown()
     {
         Debug.Log ("$$anonymous$$ouse Down");
         GameObject temp = new GameObject();
         temp = this.gameObject;
         if(temp == null)
         {
             Debug.Log ("Nothing found in the object you clicked");
         }else
         {
             transform.parent.gameObject.GetComponent<Game>().OnCardHandClick(temp);
         }
 
     }


 public void OnCardHandClick(GameObject obj)
 {
     Debug.Log ("You have clicked a card");
     
     obj.transform.parent = playerActive.transform;
 }

I think I am close to what I need to do but I keep getting the error of NullReferenceException: Object reference not set to an instance of an object. I know this error means that there is basically no information in the object or that it is null and can't do anything with it. But what I don't is how to make it so that when I click the object all of its data will be there. Due to the fact that this happens at runtime I can't drag in an instance into the inspect.

I don't know if this will help but maybe it will. Also I checked with some Debugs, it is getting inside the else statement so my problem must be the transform.parent. But again I really am confused as to what it should be.

alt text

avatar image diegzumillo · Mar 26, 2014 at 05:23 PM 0
Share

Who has this script? the player? You wrote "this" there, it means the script on the player. So you are creating a new gameobj (temp) and assigning the current component to that gameobj... doesn't make much sense. You could add a new component to the newly created gameobj though. Is this what you're trying to do?

avatar image Grinch91 · Mar 26, 2014 at 06:31 PM 0
Share

The script is within both Hand-Enemy and Hand-Player.

Yeah what you described sounds pretty much perfect. But I'm really confused about it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Raycast only works while moving 2 Answers

C# script for highlighting certain objects when the VR Controller is pointed at them? 0 Answers

Problem with Raycast 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges